gpt4 book ai didi

c# - EntityFramework 延迟加载不起作用

转载 作者:行者123 更新时间:2023-11-30 18:27:07 27 4
gpt4 key购买 nike

我是 EntityFramework 6 的新手,一直在 POCO 中遇到问题。

以下是我的代码

Domain.cs

public abstract class BaseEntity
{
[Key]
public long Id { get; set; }
}

public class User : BaseEntity
{
public long UserRoleId { get; set; }

public string Name { get; set; }

public virtual _EntityUserRole UserRole { get; set; }
}

public class UserRole : BaseEntity
{
public string Name { get; set; }

public virtual ICollection<_EntityUser> Users { get; set; }

public virtual ICollection<_EntityUserPermission> UserPermissions { get; set; }

internal _EntityUserRole()
{
Users = new List<_EntityUser>();
UserPermissions = new List<_EntityUserPermission>();
}
}

public class UserPermission : BaseEntity
{
public string Name { get; set; }

public virtual ICollection<_EntityUserRole> UserRoles { get; set; }

internal _EntityUserPermission()
{
UserRoles = new List<_EntityUserRole>();
}
}

Mapping.cs

public abstract class BaseEntityMap<T> : EntityTypeConfiguration<T> where T : BaseEntity
{
public BaseEntityMap()
{
ToTable(typeof(T).Name);
HasKey(a => a.Id);
}
}

public class UserMap : BaseEntityMap<User>
{
public UserMap()
: base()
{
HasRequired(x => x.UserRole)
.WithMany(x => x.Users)
.HasForeignKey(x => x.UserRoleId);
}
}

public class UserRoleMap : BaseEntityMap<UserRole>
{
public UserRoleMap()
: base()
{
HasMany(x => x.UserPermissions)
.WithMany(x => x.UserRoles)
.Map(x =>
{
x.MapLeftKey("UserRoleId");
x.MapRightKey("UserPermissionId");
x.ToTable("_EntityUserRole_UserPermission");
});
}
}

public class UserPermissionMap : BaseEntityMap<UserPermission>
{
public UserPermissionMap()
: base()
{
}
}

SqlServerDataContext.cs

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

public DbSet<UserRole> UserRoles { get; set; }

public DbSet<UserPermission> UserPermissions { get; set; }

public SqlServerDataContext()
: base("Name=MyDB")
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserMap());
modelBuilder.Configurations.Add(new UserRoleMap());
modelBuilder.Configurations.Add(new UserPermissionMap());

base.OnModelCreating(modelBuilder);
}
}

Program.cs

    private static void Main()
{
using (SqlServerDataContext ctx = new SqlServerDataContext())
{
UserRole role = new UserRole();
role.Name = "Super";

UserPermission prm1 = new UserPermission();
prm1.Name = "Delete";
role.UserPermissions.Add(prm1);

UserPermission prm2 = new UserPermission();
prm2.Name = "Add";
role.UserPermissions.Add(prm2);

UserPermission prm3 = new UserPermission();
prm3.Name = "Edit";
role.UserPermissions.Add(prm3);

User user = new User();
user.Name = "User 1";
user.UserRole = role;

ctx.Users.Add(user);
ctx.SaveChanges();
}

using (SqlServerDataContext ctx = new SqlServerDataContext())
{
var b = ctx.Users.Find(1);
}
}

结果 user retrieve

与打印屏幕一样,在我向User 中插入一条记录后,当我检索回来时,UserPermissions 为空。请帮忙!谢谢。

最佳答案

将模型添加到集合后,您需要SaveChanges(),以便将它们持久保存到数据库中。

然后你可以从数据库中读取你需要的东西。

之后,您需要访问该属性或使用 Include(使其变得急切,这是首选用法)。

private static void Main()
{
using (SqlServerDataContext ctx = new SqlServerDataContext())
{
UserRole role = new UserRole();
role.Name = "Super";

UserPermission prm1 = new UserPermission();
prm1.Name = "Delete";
role.UserPermissions.Add(prm1);

UserPermission prm2 = new UserPermission();
prm2.Name = "Add";
role.UserPermissions.Add(prm2);

UserPermission prm3 = new UserPermission();
prm3.Name = "Edit";
role.UserPermissions.Add(prm3);

ctx.SaveChanges();

User user = new User();
user.Name = "User 1";
user.UserRole = role;

ctx.Users.Add(user);

ctx.SaveChanges();
}

using (SqlServerDataContext ctx = new SqlServerDataContext())
{
var b = ctx.Users.Find(1);

// Method 1
var roleName = b.UserRole.Name;

// Method 2
ctx.Users.Include(x => x.UserRole).Include(x => x.UserRole.UserPermissions).FirstOrDefault(x => x.Id == 1);
}
}

关于c# - EntityFramework 延迟加载不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27357037/

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