按类型设计和插入的实体框架表

时间:2012-09-27 17:09:33

标签: entity-framework

我有一个按类型设计的表格

class Table1
{
  public int id {get;set;}
  public string data1{get;set;}
  public string data2{get;set;}
}
class Table2
{
  public int table1_id {get;set;}
  public string data3{get;set;}
  public string data4{get;set;}
}
class Table3
{
  public int table1_id {get;set;}
  public string data5{get;set;}
  public string data6{get;set;}
}

在我的种子方法中,我在Table1中插入一行。新{id = 0,...} 然后我做

context.Table2Set.AddOrUpdate(new Table2{table1_id = 0,...});

当我运行我的Update-Database时,我得到:

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
   at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

我在做什么冤屈?

更新真实数据

public class Identity
{
    public Identity()
    {
        this.Roles = new List<Role>();
    }

    public int Id { get; set; }
    public System.DateTime DateAdded { get; set; }
    public string identityprovider { get; set; }
    public string nameidentifier { get; set; }
    public System.DateTime LastLoggedIn { get; set; }

    public virtual ICollection<Role> Roles { get; set; }
}

public class Member : Identity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public Address Address { get; set; }
    public int Id { get; set; }

    public virtual ICollection<News> News { get; set; }

}
    public IdentityMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);
        this.Property(t=>t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        // Properties
        this.Property(t => t.identityprovider)
            .IsRequired()
            .HasMaxLength(255);

        this.Property(t => t.nameidentifier)
            .IsRequired();

        // Table & Column Mappings

        this.Property(t => t.Id).HasColumnName("Id");



        this.Property(t => t.DateAdded).HasColumnName("DateAdded").
            HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
        this.Property(t => t.identityprovider).HasColumnName("identityprovider");
        this.Property(t => t.nameidentifier).HasColumnName("nameidentifier");
        this.Property(t => t.LastLoggedIn).HasColumnName("LastLoggedIn")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

        // Relationships
        this.HasMany(t => t.Roles)
            .WithMany(t => t.Identities)
            .Map(m =>
                {
                    m.ToTable("IdentityRoles");
                    m.MapLeftKey("Identities_Id");
                    m.MapRightKey("Roles_Id");
                });
        this.ToTable("Identities");

    }
  public MemberMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.FirstName)
            .IsRequired()
            .HasMaxLength(150);

        this.Property(t => t.LastName)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.Email)
            .IsRequired()
            .HasMaxLength(150);

        this.Property(t => t.Address.Id)
            .IsRequired()
            .HasMaxLength(100)
            .HasColumnName("Address_FirstLine");

        this.Property(t => t.Address.ZipCode)
            .IsRequired()
            .HasMaxLength(20).HasColumnName("Address_Zip");

        this.Property(t => t.Address.Contry)
            .IsRequired()
            .HasMaxLength(100).HasColumnName("Address_Contry");

        this.Property(t => t.Address.City)
            .IsRequired()
            .HasMaxLength(100).HasColumnName("Address_Town");

        this.Property(t => t.Id);


        // Table & Column Mappings
        this.ToTable("Identities_Member");
        this.Property(t => t.FirstName).HasColumnName("FirstName");
        this.Property(t => t.LastName).HasColumnName("LastName");
        this.Property(t => t.Email).HasColumnName("Email");
        this.Property(t => t.Id).HasColumnName("Id");

        // Relationships



    }
        CreateTable(
            "dbo.Identities_Member",
            c => new
                {
                    Id = c.Int(nullable: false),
                    FirstName = c.String(nullable: false, maxLength: 150),
                    LastName = c.String(nullable: false, maxLength: 50),
                    Email = c.String(nullable: false, maxLength: 150),
                    Address_FirstLine = c.String(nullable: false, maxLength: 100),
                    Address_Street = c.String(),
                    Address_Town = c.String(nullable: false, maxLength: 100),
                    Address_Zip = c.String(nullable: false, maxLength: 20),
                    Address_Contry = c.String(nullable: false, maxLength: 100),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Identities", t => t.Id)
            .Index(t => t.Id);
}
CreateTable(
                "dbo.Identities",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        DateAdded = c.DateTime(nullable: false, defaultValueSql: "GETUTCDATE()"),
                        identityprovider = c.String(nullable: false, maxLength: 255),
                        nameidentifier = c.String(nullable: false,maxLength:100),
                        LastLoggedIn = c.DateTime(nullable: false, defaultValueSql: "GETUTCDATE()"),
                    })
                .PrimaryKey(t => t.Id)
                .Index(t=>t.nameidentifier);

0 个答案:

没有答案