Reputation: 1429
I'm using EF 4.3 and I've been making some small changes to my entities, but none of the entity classes that are a part of this error. I would like to understand why this started puking all of a sudden.
My classes:
public class Apple: Fruit
{
public Color Color{ get; set; }
}
public class Fruit: EntityBase
{
public int Size{ get; set; }
public DateTime? DateGrown{ get; set; }
public User GrownBy{ get; set; }
public User SoldBy{ get; set; }
}
public class EntityBase
{
public int Id{ get; set;}
}
The line that is now throwing "Invalid column name 'Discriminator'":
repository.Apples.Include("GrownBy").Include("SoldBy")
.Where(r => r.GrownBy.Id == user.Id
|| r.SoldBy.Id == user.Id).ToList();
If I copy the SQL that repository.Apples is attempting to run, and run it in SSMS it runs just fine:
SELECT
[Extent1].[Id] AS [Id],
'0X0X' AS [C1],
[Extent1].[DateGrown] AS [DateGrown],
[Extent1].[Color] AS [Color],
[Extent1].[DateGrown] AS [DateGrown],
[Extent1].[GrownBy_Id] AS [GrownBy_Id],
[Extent1].[SoldBy_Id] AS [SoldBy_Id],
[Extent1].[Size] AS [Size],
FROM [dbo].[Fruits] AS [Extent1]
WHERE [Extent1].[Discriminator] = N'Apple'
I tried adding the [NotMapped] Data Annotation as recommmended @ EF Code First "Invalid column name 'Discriminator'" but no inheritance, like :
[NotMapped]
public class Apple: Fruit
{
public Color Color{ get; set; }
}
but it creates a new error:
The type 'Domain.Entities.Apple' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.
on the following line:
_context.Database.Initialize(true);
Really starting to regret using EF as it keeps popping up new issues like this every week it seems. If anyone can provide any insight as to a fix, it would be greatly appreciated.
EDIT: So it looks like it has nothing to do with the Fruit or Apple entities at all. I added an inherited class of the User entity and that's what is causing things to break.
Upvotes: 1
Views: 8688
Reputation: 484
If you check migration script after entity change, you will see a new column (Discriminator) is trying to be added. You're getting this error, because you don't have this column in runtime. After you execute migrate script, error will be gone.
Upvotes: 0
Reputation: 31
First, show your dbContext. Now, try adding the column manually to the table like this:
ALTER TABLE MY_TABLE ADD MY_COLUMN NVARCHAR NULL
UPDATE [MY_TABLE] SET [MY_COLUMN]=0 WHERE [MY_COLUMN] IS NULL
ALTER TABLE MY_TABLE ALTER COLUMN MY_COLUMN NVARCHAR NOT NULL
this adds a not null column to the table, since i'm pretty sure the entity framework would have added it like that. Also, if you set the AutomaticMigrationEnabled to true, it should have done that for you (I think you should try that before the Query), like this:
internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(MyDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
As you can see, this is the Configuration.cs Class created when you enable migrations. hopefully this will help a bit.
Upvotes: 0
Reputation: 89
Looks like there is an err in mapping the SQL database to Code First or to C# code.
If you believe the SQL query is working fine through SSMS, then follow below steps. a) Open Project Solution, click Add, then New Item, then choose online, there in the search colum, type POCO..
b) Install reverse poco..make sure, initially you have your appconfig connection string name as MyDbContext.. c) once the poco class is generated, you can see your DB is mapped in C# code.
Now, as you your sql query is correct, then try with
[your_dbContext].Database.ExecuteSqlCommand(); or [your_dbContext].[TableName].SqlQuery("write your RAW SQL queries").ToList();
Upvotes: 1