无法使用EF 5拆分表 - 代码优先 - 使用现有数据库

时间:2013-08-07 12:33:50

标签: c# .net entity-framework entity-framework-5

我正在尝试使用一对一映射将旧用户表拆分为两个实体,但继续收到迁移错误,指出我的数据库不同步,即使所有内容(我认为已映射)并且我正在尝试制作一对一的关系。

这是一个现有的数据库(虽然我首先使用代码,因为迁移将变得非常重要)但我没有对数据库添加任何更改(尽管我不确定一对一表拆分的确切内容),我一直这样:

The model backing the 'Context' context has changed since the database was created.     Consider using Code First Migrations to update the database

我可以更新数据库(手动或通过迁移),但不知道实际上不同步的是什么,因为没有添加新字段且名称匹配。

BaseEntity:

public abstract class BaseEntity<T>
{
    [Key]
    public T Id { get; set; }
    public DateTime CreatedOn { get; set; }
}

会员模型:

public class Membership : BaseEntity<Guid>
{
    public string UserName { get; set; }
    public bool Approved { get; set; }
    public bool Locked { get; set; }
    public Profile Profile { get; set; }
}

个人资料模型:

public class Profile : BaseEntity<Guid>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Telephone { get; set; }
    public string Extension { get; set; }
    public Membership Membership { get; set; }
}

成员资格映射(具有1对1定义):

public class MembershipMap : EntityTypeConfiguration<Membership>
{
    public MembershipMap()
    {
        //Primary Key
        this.HasKey(t => t.Id);

        //**Relationship Mappings
        this.HasRequired(m => m.Profile)
            .WithRequiredPrincipal(p => p.Membership);

        //Properties & Column mapping
        this.Property(m => m.Id)
            .HasColumnName("PKID")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        this.Property(m => m.UserName)
            .HasColumnName("Username")
            .HasMaxLength(255);

        this.Property(m => m.Approved)
            .HasColumnName("IsApproved");

        this.Property(m => m.Locked)
            .HasColumnName("IsLocked");

        this.Property(m => m.CreatedOn)
            .HasColumnName("CreationDate");

        this.ToTable("AppUser");
    }
}

个人资料映射:

public class ProfileMap : EntityTypeConfiguration<Profile>
{
    public ProfileMap()
    {
        //Primary Key
        this.HasKey(t => t.Id);

        //Properties & Column mapping
        this.Property(m => m.Id)
            .HasColumnName("PKID")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        this.Property(m => m.FirstName)
            .HasColumnName("FirstName");

        this.Property(m => m.LastName)
            .HasColumnName("LastName");

        this.Property(m => m.Email)
            .HasColumnName("Email");

        this.Property(m => m.Telephone)
            .HasColumnName("Telephone");

        this.Property(m => m.Extension)
            .HasColumnName("Extension");

        this.ToTable("AppUser");
    }
}

数据库表 我知道并非所有字段都已映射,但在此阶段我不需要它们,当然这不是问题吗?

AppUser Database Table

1 个答案:

答案 0 :(得分:0)

问题不是Code First Mappings,而是由我切换数据库和一些胭脂迁移引起的。

要重置迁移,您可以在后续问题中找到答案:

Resetting Context for Entity Framework 5 so it thinks its working with a initialised Database - Code First

向EvilBHonda致敬

相关问题