[英].NET Entity framework generates query with Table.ModelName_Column convention
i've started working with ASP.NET MVC and using Entity Framework, concretely with SQLite db, but i've got a little problem: 我已经开始使用ASP.NET MVC并使用Entity Framework,特别是SQLite db,但是我遇到了一个小问题:
I've got a one to many relation, but EF generates wrong query, 我有一对多的关系,但是EF产生错误的查询,
SQLite error
no such column: Extent1.Category_CategoryID
so, he uses ModelName as a prefix and it's wrong. 因此,他使用ModelName作为前缀,这是错误的。 Is there any convention, which i can remove? 有没有我可以删除的约定? Or set off? 还是出发?
thx and sorry for my bad english 谢谢,我英语不好
// Update, added Category entity //更新,添加类别实体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace Eshop.Domain.Entities
{
[Table("Categories")]
public class Category
{
public Int64 CategoryID { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
}
// added Product entity //添加产品实体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace Eshop.Domain.Entities
{
[Table("Products")]
public class Product
{
public Int64 ProductID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int CategoryID { get; set; }
}
}
If you are using Code-First (which I assume) you need to... 如果您使用的是Code-First(我认为是),则需要...
either introduce a navigation property in Product
: 在Product
引入导航属性:
[Table("Products")] public class Product { public Int64 ProductID { get; set; } public string Name { get; set; } public decimal Price { get; set; } public int CategoryID { get; set; } public virtual Category Category { get; set; } }
EF will recognize CategoryID
as foreign key then. EF然后将CategoryID
识别为外键。
or configure CategoryID
as foreign key with Fluent API: 或使用Fluent API将CategoryID
配置为外键:
modelBuilder.Entity<Category>() .HasMany(c => c.Products) .WithRequired() .HasForeignKey(p => p.CategoryID);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.