实体框架映射实体到多个表? [英] Entity framework map entity to multiple tables?
问题描述
表用户:用户名,密码,RoleId,IsActive,CreatedDate,ActivedDate
表管理员:用户名,名称
表格员工:用户名,姓名,职务,电话
从这个数据库模式,我想通过合并表数据生成以下实体:
public class User
{
[Key]
public string Username {get;组; }
public string Password {get;组; }
public int RoleId {get;组; }
public bool IsActive {get;组; }
public DateTime CreatedDate {get;组; }
public DateTime ActivedDate {get;组; }
public string Name {get;组; }
public string Phone {get;组; }
public string Position {get;组; }
[ForeignKey(RoleId)]
public Role Role {get;组; }
}
配置类:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< User>()
.Map(map =>
{
map.Properties(p => new
{
p.Username,
p.Password,
p.RoleId,
p
$ b p.CreatedDate,
p.ActivedDate
});
map.ToTable(User);
})
.Map (map =>
{
map.Properties(p => new
{
p.Username,
p.Name
});
map.ToTable(Admin);
})。Map(map =>
{
map.Properties p =>新
{
p.Username,
p.Name,
p.Phone,
p.Position
});
map.ToTable(Staff);
});
base.OnModelCreating(modelBuilder);
}
我已经测试过,但没有按预期工作。我总是收到以下消息:
用户类型的属性只能映射一次。非关键属性名称被映射不止一次。确保属性方法仅指定每个非密钥属性一次。
我是否缺少某些东西?
问题是在 Admin $ c $中的两个
名称
c>和 Staff
当然不能同时映射到中的
。名称
属性
但是您可以有两个不同的名称属性,并将两者映射到两个表中的相同列名称:
用户
中的更改:
public用户
{
[Key]
public string Username {get;组; }
...
public string AdminName {get;组; }
public string StaffName {get;组; }
...
}
映射片段:
.Map(map =>
{
map.Properties(p => new
{
p.Username,
p.AdminName
});
map.Property(p => p.AdminName).HasColumnName(Name);
map.ToTable(Admin);
})。Map(map =>
{
map.Properties(p => new
{
p.Username,
p.StaffName,
p.Phone,
p.Position
});
map.Property(p => p.StaffName) .HasColumnName(Name);
map.ToTable(Staff);
});
I'm facing a problem using EF. I have the following situation:
Table User: Username, Password, RoleId, IsActive, CreatedDate, ActivedDate
Table Admin: Username, Name
Table Staff: Username, Name, Position, Phone
From this database schema i'd like to generate the following entity by merge tables data:
public class User
{
[Key]
public string Username { get; set; }
public string Password { get; set; }
public int RoleId { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime ActivedDate { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Position { get; set; }
[ForeignKey("RoleId")]
public Role Role { get; set; }
}
configuration class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.Map(map =>
{
map.Properties(p => new
{
p.Username,
p.Password,
p.RoleId,
p.IsActive,
p.CreatedDate,
p.ActivedDate
});
map.ToTable("User");
})
.Map(map =>
{
map.Properties(p => new
{
p.Username,
p.Name
});
map.ToTable("Admin");
}).Map(map =>
{
map.Properties(p => new
{
p.Username,
p.Name,
p.Phone,
p.Position
});
map.ToTable("Staff");
});
base.OnModelCreating(modelBuilder);
}
I've tested it but it doesn't work as expected. I always get this message:
Properties for type 'User' can only be mapped once. The non-key property 'Name' is mapped more than once. Ensure the Properties method specifies each non-key property only once.
Am I missing something?
The problem is that the two Name
s in Admin
and Staff
of course can't both be mapped onto one Name
property in User
.
But you can have two different name properties and map both to the same column names in two tables:
Changes in User
:
public class User
{
[Key]
public string Username { get; set; }
...
public string AdminName { get; set; }
public string StaffName { get; set; }
...
}
And the mapping fragments:
.Map(map =>
{
map.Properties(p => new
{
p.Username,
p.AdminName
});
map.Property(p => p.AdminName).HasColumnName("Name");
map.ToTable("Admin");
}).Map(map =>
{
map.Properties(p => new
{
p.Username,
p.StaffName,
p.Phone,
p.Position
});
map.Property(p => p.StaffName).HasColumnName("Name");
map.ToTable("Staff");
});
这篇关于实体框架映射实体到多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!