实体框架无效列(数据库或代码中不存在列)

时间:2015-06-17 01:04:48

标签: c# .net entity-framework

所以我见过像我这样的很多问题。但没有一个答案是有效的。 每当我尝试更新实体时,我都会收到无效列异常:

  

[SqlException(0x80131904):列名称无效' ssma $ rowid'。   列名称无效' ssma $ rowid'。]

这是从哪里来的?我有重新生成/重建/清理/删除表,但仍然是错误。数据库没有具有此名称的列。我该如何解决这个问题?

这是我的实体,它没有外键:

public interface IDataContext : IDisposable
{
    IDbSet<Organization> Organizations { get; set; } // organizations
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new OrganizationConfiguration());
}
public partial class Organization : BaseEntity
{
    public long Id { get; set; } // id (Primary key)
    public long? ReportTypeId { get; set; } // report_type_id
    public byte Active { get; set; } // active
    public long? TimeZoneId { get; set; } // time_zone_id
    public string Name { get; set; } // name
    public string MainContact { get; set; } // main_contact
    public string Address { get; set; } // address
    public string City { get; set; } // city
    public string StateId { get; set; } // state_id
    public string StateName { get; set; } // state_name
    public string ZipCode { get; set; } // zip_code
    public string Phone { get; set; } // phone
    public string Fax { get; set; } // fax
    public string Email { get; set; } // email
    public string Website { get; set; } // website
    public short IncludeContactType { get; set; } // include_contact_type
    public short NursingNote { get; set; } // nursing_note
    public DateTime UpdatedAt { get; set; } // updated_at
    public DateTime CreatedAt { get; set; } // created_at
    public string OfficeBackline { get; set; } // officeBackline
    public string Pharmacy { get; set; } // pharmacy
    public string PharmacyPhone { get; set; } // pharmacyPhone
    public string PharmacyBin { get; set; } // pharmacyBin
    public string PharmacyGroup { get; set; } // pharmacyGroup
    public string Dme { get; set; } // dme
    public string DmePhone { get; set; } // dmePhone
    public string LanguageLine { get; set; } // languageLine
    public string LanguageLinePin { get; set; } // languageLinePin
    public string Announcements { get; set; } // announcements
    public string Skillname { get; set; } // skillname
    public string OutboundSkill { get; set; } // outbound_skill
    public string InboundSkill { get; set; } // inbound_skill
    public string Dnis { get; set; } // dnis
    public byte[] Logo { get; set; } // logo

    public Organization()
    {
        Active = 1;
        StateId = "NULL";
        StateName = "N''";
        NursingNote = 0;
        UpdatedAt = DateTime.Now;
        OfficeBackline = "NULL";
        Pharmacy = "NULL";
        PharmacyPhone = "NULL";
        PharmacyBin = "NULL";
        PharmacyGroup = "NULL";
        Dme = "NULL";
        DmePhone = "NULL";
        LanguageLine = "NULL";
        LanguageLinePin = "NULL";
        Skillname = "NULL";
        OutboundSkill = "NULL";
        InboundSkill = "NULL";
        Dnis = "NULL";
    }
}

以下是配置:

// organizations
internal partial class OrganizationConfiguration : EntityTypeConfiguration<Organization>
{
    public OrganizationConfiguration()
    {
        ToTable("dbo.organizations");
        HasKey(x => x.Id);

        Property(x => x.Id).HasColumnName("id").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(x => x.ReportTypeId).HasColumnName("report_type_id").IsOptional();
        Property(x => x.Active).HasColumnName("active").IsRequired();
        Property(x => x.TimeZoneId).HasColumnName("time_zone_id").IsOptional();
        Property(x => x.Name).HasColumnName("name").IsRequired().HasMaxLength(255);
        Property(x => x.MainContact).HasColumnName("main_contact").IsRequired().HasMaxLength(255);
        Property(x => x.Address).HasColumnName("address").IsRequired().HasMaxLength(255);
        Property(x => x.City).HasColumnName("city").IsRequired().HasMaxLength(255);
        Property(x => x.StateId).HasColumnName("state_id").IsOptional().HasMaxLength(5);
        Property(x => x.StateName).HasColumnName("state_name").IsRequired().HasMaxLength(255);
        Property(x => x.ZipCode).HasColumnName("zip_code").IsRequired().HasMaxLength(255);
        Property(x => x.Phone).HasColumnName("phone").IsRequired().HasMaxLength(255);
        Property(x => x.Fax).HasColumnName("fax").IsRequired().HasMaxLength(255);
        Property(x => x.Email).HasColumnName("email").IsRequired().HasMaxLength(255);
        Property(x => x.Website).HasColumnName("website").IsRequired().HasMaxLength(255);
        Property(x => x.IncludeContactType).HasColumnName("include_contact_type").IsRequired();
        Property(x => x.NursingNote).HasColumnName("nursing_note").IsRequired();
        Property(x => x.UpdatedAt).HasColumnName("updated_at").IsRequired();
        Property(x => x.CreatedAt).HasColumnName("created_at").IsRequired();
        Property(x => x.OfficeBackline).HasColumnName("officeBackline").IsOptional().HasMaxLength(50);
        Property(x => x.Pharmacy).HasColumnName("pharmacy").IsOptional().HasMaxLength(255);
        Property(x => x.PharmacyPhone).HasColumnName("pharmacyPhone").IsOptional().HasMaxLength(50);
        Property(x => x.PharmacyBin).HasColumnName("pharmacyBin").IsOptional().HasMaxLength(50);
        Property(x => x.PharmacyGroup).HasColumnName("pharmacyGroup").IsOptional().HasMaxLength(255);
        Property(x => x.Dme).HasColumnName("dme").IsOptional().HasMaxLength(255);
        Property(x => x.DmePhone).HasColumnName("dmePhone").IsOptional().HasMaxLength(50);
        Property(x => x.LanguageLine).HasColumnName("languageLine").IsOptional().HasMaxLength(255);
        Property(x => x.LanguageLinePin).HasColumnName("languageLinePin").IsOptional().HasMaxLength(50);
        Property(x => x.Announcements).HasColumnName("announcements").IsOptional();
        Property(x => x.Skillname).HasColumnName("skillname").IsOptional().HasMaxLength(255);
        Property(x => x.OutboundSkill).HasColumnName("outbound_skill").IsOptional().HasMaxLength(30);
        Property(x => x.InboundSkill).HasColumnName("inbound_skill").IsOptional().HasMaxLength(30);
        Property(x => x.Dnis).HasColumnName("dnis").IsOptional().HasMaxLength(30);
        Property(x => x.Logo).HasColumnName("logo").IsOptional();
    }
}

和datacontext类

public partial class DataContext : DbContext, IDataContext
{
    public override int SaveChanges()
    {
        var changedEntities = ChangeTracker.Entries();
        foreach (var changedEntity in changedEntities)
        {
            if (changedEntity.Entity is BaseEntity)
            {
                var entity = (BaseEntity)changedEntity.Entity;
                switch (changedEntity.State)
                {
                    case EntityState.Added:
                        entity.OnBeforeInsert();
                        break;
                    case EntityState.Modified:
                        entity.OnBeforeUpdate();
                        break;
                    case EntityState.Deleted:
                        entity.OnBeforeDelete();
                        break;
                }
            }
        }
        var results = base.SaveChanges();
        return results;
    }
}    

2 个答案:

答案 0 :(得分:1)

这不是实体框架问题。它是数据库中的一些东西,遗憾的是,你没有给我们任何细节,甚至不是RDBMS。

听起来你有一个从其他RDBMS迁移的SQL Server数据库。

我怀疑该问题源自其中一个引用此ssma$rowid列的表触发器。浏览触发器,看看是否可以找到它,并根据需要进行修复。

这个ssma$rowid列听起来就像数据库迁移时遗留下来一样。

我希望这会有所帮助。如果您添加有关表,触发器,数据库等的更多详细信息,我们可能会为您提供进一步的帮助。

答案 1 :(得分:0)

该列由Oracle到SQL转换器生成 这个错误可能与映射有关(这是你唯一的映射吗?你有没有DBFirst文件?)或者像@sstan所建议的那样仍然在数据库中。