我正在创建一个示例ASP.NET MVC Web应用程序,并且正在遵循数据库的代码优先方法。我想创建products
表和transactions
表,此外,我想通过迁移包括一些示例数据,但是当我尝试这样做时,Update-Database
我得到了标题中提到的错误消息。我确切知道错误发生的原因,这是因为我使用List<Product>
,如下所示。但是,我不知道该如何解决该问题,而交易应包括一个或多个产品。我的代码段可以在下面找到。
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
}
public class Transaction
{
public int TransactionID { get; set; }
public List<Product> Products { get; set; }
}
我还在文件中添加了以下代码行IdentityModels.cs
:
public DbSet<Product> Products { get; set; }
public DbSet<Transaction> Transactions { get; set; }
最后,Configuration.cs
保存迁移的文件如下所示:
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(MyApp.Models.ApplicationDbContext context)
{
var pr = new List<Product>();
pr.Add(new Product { Name = "Book" });
pr.Add(new Product { Name = "Table" });
pr.Add(new Product { Name = "Chair" });
pr.ForEach(i => context.Products.AddOrUpdate(p => p.Name, i));
context.SaveChanges();
context.Transactions.AddOrUpdate(
t => t.Products,
new Transaction { Products = new List<Product>(pr.Where(p => p.Name == "Book" || p.Name == "Table")) },
new Transaction
{
Products = new List<Product>(pr.Where(p => p.Name == "Chair" || p.Name == "Book" || p.Name == "Table"))
}
);
context.SaveChanges();
}
问题是AddOrUpdate
方法的第一个参数,即identifierExpression
。您应该在此处提供基本类型,该基本类型确定何时要更新和何时添加。如果数据库中的一行匹配,identifierExpression
它将用您提供的新行进行更新。如果没有,新的将被插入到数据库中。
您用作t.Products
标识符,这意味着,当您要添加Products
的数据库与数据库行之一相同时,应该进行更新,因为Products
没有原始类型,所以更新不正确。因此,您可以提供原始类型属性或根本不使用此参数(这意味着将插入所有项目)。
context.Transactions.AddOrUpdate(
//t => t.Products, //comment this
new Transaction {
Products = new List<Product>(
pr.Where(p => p.Name == "Book" || p.Name == "Table"))
},
new Transaction
{
Products = new List<Product>(
pr.Where(p => p.Name == "Chair" || p.Name == "Book" || p.Name == "Table"))
}
);
建议
从您的Seed
方法中可以推断出Transaction
和之间的关系Product
是多对多的。如果是这种情况,则应为EF指定它。根据您当前的模型,该关系是一对多的。您可以像这样在Context
类中更改它:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Transaction>().HasMany(x => x.Products).WithMany();
}
附带说明一下,如果要向模型添加延迟加载,则应声明as的Products
属性。Transaction
virtual
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句