实体框架 - 复杂类型不需要在Context中定义

时间:2013-04-15 18:51:24

标签: entity-framework-5

我在使用以下错误启用我的应用中的迁移时遇到了复杂的类型错误:

The type 'eCommerceDataModel.Models.UserImage' has already been configured as an entity type. 
It cannot be reconfigured as a complex type.

只是分享这个,因为你可能会遇到这个愚蠢的问题。 使用复杂类型时,请不要在Context Class中定义复杂类型实体。

public class eComContext : DbContext
{
    public eComContext() : base("eCommDB") { }
    public IDbSet<SiteSettings> SiteSettings { get; set; }
    public IDbSet<User> Users { get; set; }
    //public IDbSet<UserImage> UserImage { get; set; } (This needs to be removed)
}

[Table("SiteDetails")]
public class SiteSettings
{
    [Key]
    public int SiteId { get; set; }
    [MaxLength(30), MinLength(5)] 
    public string Name { get; set; }
    [MaxLength(50), MinLength(10)] 
    public string PunchLine { get; set; }
    public string Description { get; set; }
    public byte[] Logo { get; set; }
    public virtual User CreatedBy { get; set; }
    public virtual User UpdatedBy { get; set; }
}

[Table("SiteUsers")]
public class User
{
    [Key]
    public int UserId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public byte Password { get; set; }
    public UserImage UserImage { get; set; }
}

[ComplexType]
public class UserImage
{
    public byte[] PhotoSmall { get; set; }
    public byte[] PhotoMedium { get; set; }
    public byte[] PhotoLarge { get; set; }
}

需要删除Context类中的UserImage属性以解决此错误。现在我可以启用迁移。

0 个答案:

没有答案