Entity framework EF代码在多对多中处于第一位
我有以下课程:Entity framework EF代码在多对多中处于第一位,entity-framework,ef-code-first,mapping,many-to-many,Entity Framework,Ef Code First,Mapping,Many To Many,我有以下课程: public class User { public int Id {get; set;} public string Name {get; set;} } 数据库表-用户 public class Pet { public int Id {get; set;} public string Name {get; set;} } public class UsersPets { public int UserId {get; set;} public int Pet
public class User
{
public int Id {get; set;}
public string Name {get; set;}
}
数据库表-用户
public class Pet
{
public int Id {get; set;}
public string Name {get; set;}
}
public class UsersPets
{
public int UserId {get; set;}
public int PetId {get; set;}
}
DB表格-宠物
public class Pet
{
public int Id {get; set;}
public string Name {get; set;}
}
public class UsersPets
{
public int UserId {get; set;}
public int PetId {get; set;}
}
数据库表-用户\u宠物
public class Pet
{
public int Id {get; set;}
public string Name {get; set;}
}
public class UsersPets
{
public int UserId {get; set;}
public int PetId {get; set;}
}
到目前为止,我可以用linq获得用户的宠物。但是如何自动映射
User.Pets
,而不首先在EF代码中添加Linq查询 你就不能把你的课程改成:
public class User
{
public User(){
Pets = new HashSet<Pet>();
}
public int Id {get; set;}
public string Name {get; set;}
public ICollection<Pet> Pets;
}
public class Pet
{
public Pet(){
Users = new HashSet<User>();
}
public int Id {get; set;}
public string Name {get; set;}
public ICollection<User> Users;
}
公共类用户
{
公共用户(){
Pets=新HashSet();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公众收集宠物;
}
公营宠物
{
公共宠物(){
Users=newhashset();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection用户;
}
你能不能把你的课程改成:
public class User
{
public User(){
Pets = new HashSet<Pet>();
}
public int Id {get; set;}
public string Name {get; set;}
public ICollection<Pet> Pets;
}
public class Pet
{
public Pet(){
Users = new HashSet<User>();
}
public int Id {get; set;}
public string Name {get; set;}
public ICollection<User> Users;
}
公共类用户
{
公共用户(){
Pets=新HashSet();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公众收集宠物;
}
公营宠物
{
公共宠物(){
Users=newhashset();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection用户;
}
EF为您创建此表,您不应在您的模型中执行此操作。因此:
public class User
{
public int Id {get; set;}
public string Name {get; set;}
public ICollection<Pet> Pets {get; set;}
}
public class Pet
{
public int Id {get; set;}
public string Name {get; set;}
}
公共类用户
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection宠物{get;set;}
}
公营宠物
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
附加表将在数据库中创建,您可以在代码中访问用户实体的Pets集合。EF为您创建此表,您不应在模型中执行此操作。因此:
public class User
{
public int Id {get; set;}
public string Name {get; set;}
public ICollection<Pet> Pets {get; set;}
}
public class Pet
{
public int Id {get; set;}
public string Name {get; set;}
}
公共类用户
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection宠物{get;set;}
}
公营宠物
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
附加表将在数据库中创建,您可以访问代码中用户实体的Pets集合。对于普通的多对多关系,您不需要额外的类,只需向
用户
和Pet
类添加两个属性即可:
public class User
{
public int Id {get; set;}
public string Name {get; set;}
public virtual ICollection<Pet> Pets { get; set; }
public User
{
Pets = new List<Pet>();
}
}
public class Pet
{
public int Id {get; set;}
public string Name {get; set;}
public virtual ICollection<User> Users { get; set; }
public Pet
{
Users = new List<User>();
}
}
对于简单的多对多关系,您不需要额外的类,只需向
用户
和宠物
类添加两个属性即可:
public class User
{
public int Id {get; set;}
public string Name {get; set;}
public virtual ICollection<Pet> Pets { get; set; }
public User
{
Pets = new List<Pet>();
}
}
public class Pet
{
public int Id {get; set;}
public string Name {get; set;}
public virtual ICollection<User> Users { get; set; }
public Pet
{
Users = new List<User>();
}
}
在此处查看我关于导航属性的文章:您需要描述这些实体如何使用导航属性进行关联。在此处查看我关于导航属性的文章:您需要描述这些实体如何使用导航属性进行关联。