2
我在现有的SQL数据库上使用EF 4.2 Code First时遇到问题。EF代码优先,静态复合外键部分
我有多个实体等企业,用户等
通用信息被存储在称为实体与主密钥[ENTITYID,EntityTypeId]一个seprate表。
- ENTITYID对应于CorporateId,用户ID等
- EntityTypeId是静态ID,用于像discrimiator实体
我有下面的类设计之间。
public class User
{
public int Id {get;set;}
public int EntityTypeId { get { return 1; } set { }}
public Entity Entity {get;set;}
}
public class Entity
{
public int Id {get;set;}
public int EntityTypeId {get;set;}
}
正如您所见,EntityTypeId是静态属性,并且用户表中没有对应的列。
地图:
public UserMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Table & Column Mappings
this.ToTable("User");
this.Property(t => t.Id).HasColumnName("UserId");
// Relationships
this.HasRequired(t => t.Entity)
.WithMany()
.HasForeignKey(p => new { p.Id, p.EntityTypeId }); //<-- Problem
}
public EntityMap()
{
// Primary Key
this.HasKey(t => new { t.Id, t.EntityTypeId });
// Table & Column Mappings
this.ToTable("Entity");
this.Property(t => t.Id).HasColumnName("EntityId");
this.Property(t => t.EntityTypeId).HasColumnName("EntityTypeId");
}
当我运行这段代码,我得到的错误: System.Data.SqlClient.SqlException:无效的列名称EntityTypeId“。
我认为这是因为EntityTypeId列在用户表中不存在。 有没有解决这个问题的方法,因为我无法在表格上创建此列?!
非常感谢