2

在我的应用程序中启用迁移时,我遇到了复杂类型错误,但出现以下错误:

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

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

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 属性才能解决此错误。现在能够启用迁移。

4

0 回答 0