gpt4 book ai didi

entity-framework - Entity Framework 4.1代码优先自引用一对多和多对多关联

转载 作者:行者123 更新时间:2023-12-03 14:00:57 26 4
gpt4 key购买 nike

我有一个可以收集他喜欢的用户的用户...

另一个用户可以收集他喜欢的用户。

如果用户A喜欢用户B,如果用户B喜欢用户A,那么他们就可以出去玩了。我需要互相发送联系方式。我们如何在Entity Framework Code First中表示这样的模型?

public class User
{
public int UserId { get; set; }

public int? UserLikeId { get; set; }
public virtual UserLike UserLike { get; set; }
}

public class UserLike
{
public int UserLikeId { get; set; }

public int UserId { get; set; }
public virtual User User { get; set; }

public virtual ICollection<User> LikeUsers { get; set; }
}


这个模型正确吗?我无法使它正常工作。

我尝试了另一种方法,但是那也行不通...

我试图将用户集合添加到用户表。

对于前:

public virtual ICollection<User> userlike { get; set; }

public class User
{
public int UserId { get; set; }
public virtual ICollection<UserLike> UserLikes { get; set; }
}

public class UserLike
{
public int UserLikeId { get; set; }

public int UserId { get; set; }
public virtual User User { get; set; }

public int LikeUserId { get; set; }
public virtual User LikeUser { get; set; }
}


当我尝试添加用户及其喜欢的人时,出现此错误:


已检测到关系“ UserLike_LikeUser”的角色“ UserLike_LikeUser_Target”的冲突更改。


代表这种模型的最佳方法是什么?

最佳答案

您实际上并不需要一个单独的实体来描述这种关系,下面的对象模型可以解决这个问题:

public class User
{
public int UserId { get; set; }
public string Name { get; set; }

public int? ThisUserLikesId { get; set; }
public virtual User ThisUserLikes { get; set; }
public virtual ICollection<User> LikeThisUser { get; set; }
}

public class Context : DbContext
{
public DbSet<User> Users { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasOptional(u => u.ThisUserLikes)
.WithMany(u => u.LikeThisUser)
.HasForeignKey(u => u.ThisUserLikesId);
}
}


现在,假设您手中有一个UserId,并希望找到另一个喜欢该用户的User,而该用户也喜欢他:

using (var context = new Context())
{
// For a given user id = 1
var friends = (from u in context.Users
where u.UserId == 1
from v in u.LikeThisUser
where v.UserId == u.ThisUserLikesId
select new
{
OurUser = u,
HerFriend = v
})
.SingleOrDefault();

ExchangeContactInfo(friends.OurUser, friends.HerFriend);
}




更新1:

使用连接表将自引用的多对多关联映射到数据库,该连接表需要不同的对象模型和流利的API:

public class User
{
public int UserId { get; set; }
public string Name { get; set; }

public virtual ICollection<User> ThisUserLikes { get; set; }
public virtual ICollection<User> UsersLikeThisUser { get; set; }
}

public class Context : DbContext
{
public DbSet<User> Users { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasMany(u => u.ThisUserLikes)
.WithMany(u => u.UsersLikeThisUser)
.Map(c =>
{
c.MapLeftKey("UserId");
c.MapRightKey("OtherUserId");
c.ToTable("UserLikes");
});
}
}




更新2:

正如我在 this post中所解释的,多对多关联不能具有有效负载(例如EventId),如果是这种情况,那么我们就必须将其分解为两个一对多的关联,从而成为中间类。可以看到您已经正确创建了该类(UserLike)以表示附加到自引用多对多关联的额外信息,但是该中间类的关联不正确,因为我们需要准确地定义2个多对多如我在以下对象模型中所示,从UserLike到User的一种关联:

public class User
{
public int UserId { get; set; }
public string Email { get; set; }

public virtual ICollection ThisUserLikes { get; set; }
public virtual ICollection UsersLikeThisUser { get; set; }
}

public class UserLike
{
public int UserLikeId { get; set; }
public int LikerId { get; set; }
public int LikeeId { get; set; }
public int EventId { get; set; }

public User Liker { get; set; }
public User Likee { get; set; }
public virtual Event Event { get; set; }
}

public class Event
{
public int EventId { get; set; }
public string Name { get; set; }
}

public class Context : DbContext
{
public DbSet Users { get; set; }
public DbSet Events { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(u => u.ThisUserLikes)
.WithRequired(ul => ul.Liker)
.HasForeignKey(ul => ul.LikerId);
modelBuilder.Entity()
.HasMany(u => u.UsersLikeThisUser)
.WithRequired(ul => ul.Likee)
.HasForeignKey(ul => ul.LikeeId)
.WillCascadeOnDelete(false);
}
}


现在,您可以使用以下LINQ查询来检索彼此喜欢的所有用户:

using (var context = new Context())
{
var friends = (from u1 in context.Users
from likers in u1.UsersLikeThisUser
from u2 in u1.ThisUserLikes
where u2.LikeeId == likers.LikerId
select new
{
OurUser = u1.UserId,
HerFriend = u2.LikeeId
})
.ToList();
}


希望这可以帮助。

关于entity-framework - Entity Framework 4.1代码优先自引用一对多和多对多关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6383576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com