我是代码第一范式的新手,所以请多多包涵。我无法从我的实体类中获取在我的数据库上创建的基元或自定义类的集合/列表/数组。因此,我无法正确地为表播种。没有错误,只需获取一个包含除我的集合之外的所有属性的实体表。我正在使用 EF5、VS2010、MVC4 和 SqlExpress。我究竟做错了什么?
我的实体类:
public class MyEntity
{
public int MyEntityID { get; set; } // GOOD
public string Property1 { get; set; } // GOOD
public bool Property2 { get; set; } // GOOD
public IList<CustomClass> CustomClassList { get; set; } //BAD, NOT CREATED ON DB
public CustomClass[] CustomClassArray { get; set; } //BAD, NOT CREATED ON DB
}
我的自定义类:
[ComplexType]
public class CustomClass
{
public string Title { get; set; }
}
我的配置/迁移类
public class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(MyContext context)
{
context.MyEntity.AddOrUpdate(x => x.MyEntityID,
new MyEntity()
{
Property1 = "abc",
Property2 = "xyz",
CustomClassList = new List<CustomClass> {new CustomClass{Title="Help"}}
}
context.SaveChanges();
}
}
我的 Global.asax.cs Application_Start() 方法
protected void Application_Start()
{
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
}
当我尝试创建一个新数据库时,我得到了相同的结果:
<!--NO LUCK-->
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS2;Initial Catalog=TESTDB_2;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<!--NO LUCK-->
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS2;Initial Catalog=TESTDB_3;Integrated Security=SSPI" providerName="System.Data.SqlClient" />