0

I have a simple app with zero inheritance (at least as far as the database is concerned). How can I "tell" this to Entity Framework so that it would stop looking for the Discriminator column?

I don't have access to change the structure of the database tables, but I need to change the data, and I can't do it because EF keeps trying to operate on the non-existent Discriminator column and crashing as a result.

Edit: Added code

public class DatabaseContext : DbContext {
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Entity<User>().ToTable("Users");
    }
}


public class UserRepository {
    public int Insert(User entity) {
        using (var db = new DatabaseContext()) {
            var table = db.Users;
            table.Add(entity);
            return db.SaveChanges();
        }
    }
}

public class User : IEntity {
    public int Id { get; set; }

    public String Username { get; set; }

    public String Password { get; set; }

    public String FullName { get; set; }

    public String Email { get; set; }
}

public interface IEntity {
    int Id { get; set; }
}
4

1 回答 1

0

如果您在 DbContext 上使用 Table per Hierarchy 继承,EF 只会查找 Discriminator 列。TPH DbContext 类的示例如下:

public abstract class Animal 
{
    public int AnimalId { get; set; }
    public string Name { get; set; }        
}

public class Cat : Animal
{
    public string Race { get; set; }
}

public class Dog : Animal
{
    public string BarkStyle { get; set; }
}

public class InheritanceMappingContext : DbContext
{
    public DbSet<Animal> Animal { get; set; }
}

你必须放弃这种模式。

于 2013-03-22T17:39:06.287 回答