我正在使用Visual Studio 2012,MVC 4和Razor(CSHTML)。我在测试数据库中创建了Person.Person表。我想让我的模型PersonModels.cs使用此表,所以我创建了以下2个类。
public class Person
{
[Key]
public int BusinessEntityID { get; set; }
public string PersonType { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Suffix { get; set; }
public int EmailPromotion { get; set; }
[XmlAttribute(DataType = "string")]
public string AdditionalContactInfo { get; set; }
[XmlAttribute(DataType = "string")]
public string Demographics { get; set; }
public string rowguid { get; set; }
public DateTime ModifiedDate { get; set; }
}
public class PersonDbContext : DbContext
{
public DbSet<Person> person { get; set; }
}
我以为它会选择Person.Person表,因为类名是Person,即使我没有包括模式。但是,在运行应用程序并执行插入操作时,我检查了Person.Person表,但该行不存在。它创建了dbo.People表,并在其中插入了行!我仔细检查了一下,因为我以为自己可能会喝醉,但是我没有在任何地方写人!
我阅读了有关反向poco的文章,但我想了解它的工作原理而不是使其工作。
编辑:它起作用了!请找到我使用的附加代码:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace client_site.Models
{
[Table("Person", Schema = "Person")]
public class TestModel
{
[Key]
public int ID { get; set; }
}
public class DefaultConnectionX : DbContext
{
public DbSet<TestModel> test { get; set; }
}
}
dbo是默认的数据库架构。如果要在人员模式中创建表,则必须添加一个属性
[Table(“ Person”,Schema =“ Person”)]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句