Entity Framework 4.1 Code First 和一对多映射问题
我在映射现有数据库时遇到问题。
2 个表(简化)
"SomeEntity"
Id int
Name nvarchar
和
"EntityProperty"
EntityId int
Name nvarchar
具有从实体到实体属性的一对多关系。
如何使用 EF 4.1 Code First 映射此内容?
提前谢谢。
编辑1:
好的)这是我的代码
class Program
{
static void Main(string[] args)
{
var context = new DataContext();
var result = context.SomeEntity.Include(p => p.EntityProperties);
foreach (var entity in result)
{
Console.WriteLine(entity);
}
}
}
public class SomeEntity
{
public int EntityId { get; set; }
public string Name { get; set; }
public virtual ICollection<EntityProperty> EntityProperties { get; set; }
public override string ToString()
{
return string.Format("Id: {0}, Name: {1}", EntityId, Name);
}
}
public class EntityProperty
{
public int EntityId { get; set; }
public string Name { get; set; }
}
public class DataContext : DbContext
{
public DbSet<SomeEntity> SomeEntity { get { return this.Set<SomeEntity>(); } }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<SomeEntity>().ToTable("SomeEntity");
modelBuilder.Entity<SomeEntity>().HasKey(k => k.EntityId);
modelBuilder.Entity<EntityProperty>().ToTable("EntityProperty");
modelBuilder.Entity<EntityProperty>().HasKey(k => k.EntityId);
}
}
当我使用“包含在查询中获取属性”时出现问题:
列名“SomeEntity_EntityId”无效。 列名“SomeEntity_EntityId”无效。
I have problem with mapping existing database.
2 tables (simplified)
"SomeEntity"
Id int
Name nvarchar
and
"EntityProperty"
EntityId int
Name nvarchar
and have relation one-to-many from Entity to entity properties.
How I can map this using EF 4.1 Code First?
Thx in advance.
Edited 1:
ok) this is my code
class Program
{
static void Main(string[] args)
{
var context = new DataContext();
var result = context.SomeEntity.Include(p => p.EntityProperties);
foreach (var entity in result)
{
Console.WriteLine(entity);
}
}
}
public class SomeEntity
{
public int EntityId { get; set; }
public string Name { get; set; }
public virtual ICollection<EntityProperty> EntityProperties { get; set; }
public override string ToString()
{
return string.Format("Id: {0}, Name: {1}", EntityId, Name);
}
}
public class EntityProperty
{
public int EntityId { get; set; }
public string Name { get; set; }
}
public class DataContext : DbContext
{
public DbSet<SomeEntity> SomeEntity { get { return this.Set<SomeEntity>(); } }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<SomeEntity>().ToTable("SomeEntity");
modelBuilder.Entity<SomeEntity>().HasKey(k => k.EntityId);
modelBuilder.Entity<EntityProperty>().ToTable("EntityProperty");
modelBuilder.Entity<EntityProperty>().HasKey(k => k.EntityId);
}
}
Problem when I use Include in query for get properties:
Invalid column name 'SomeEntity_EntityId'.
Invalid column name 'SomeEntity_EntityId'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建 ICollection(在关系的“1”侧)应该足以设置 1:N 关系。它将在 EntityProperty 表中创建 SomeEntity_Id (或 SomeEntityId)列。
编辑:顺便说一句:如果您想启用延迟加载,您可以将该集合设置为虚拟。
编辑:
首先尝试这个..然后您可以再次添加该 ICollection,这次我没有包含它以保持简单(并且您仍然查询属性..但是使用:
context.EntityProperties.Where(x= >x.EntityId == X);
)Creating that ICollection (at '1' side of relation) should be enough to set 1:N relation. It will create SomeEntity_Id (or SomeEntityId) column in EntityProperty table.
Edit: Btw: You can set that collection to virtual, if you want lazy loading enabled.
Edit:
First try this.. then you can add that ICollection again, I didn't include it this time to keep it simple (and you an still query properties.. but with:
context.EntityProperties.Where(x=>x.EntityId == X);
)我解决了问题。
我无法将简单的 PK 添加到关系表中。我在所有独特的字段上添加了复杂的 PK 并映射一对多。就这样。
I solved problem.
I cannot add simple PK to relational table. I added complex PK on all unique fields and map one-to-many. That's all.