기존 데이터베이스 매핑에 문제가 있습니다. 간체Entity Framework 4.1 코드 우선 및 일대 다 매핑 문제
2 테이블
"SomeEntity"
Id int
Name nvarchar
및
"EntityProperty"
EntityId int
Name nvarchar
과 관계가 일대 엔티티로부터 엔티티 속성.
어떻게 EF 4.1 코드를 사용하여 매핑 할 수 있습니까?
Thx. 편집 한
: 속성을 가져올 내가 쿼리에 포함 사용할 때
확인)이 내 코드
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'열 이름이 잘못되었습니다.
'SomeEntity' 클래스에서'EntityId' 대신'Id'를 사용하지 않아야합니까? 그것도 매핑을 변경하십시오. – Damb
Id에서는 동일한 예외가 있지만 다른 메시지가 있습니다. 'SomeEntity_Id'열 이름이 잘못되었습니다. 'SomeEntity_Id'열 이름이 잘못되었습니다. – Gengzu
답변을 수정했습니다. 만약 내가 자고있는 동안 작동시키지 않았다면 시도해보십시오 :) – Damb