Reputation: 494
I have some database that goes quite against most standards (e.g. some tables have no PKs, some FKs point nowhere...). One entity, let's call it entity1 has several fields, field1, Field2 and field_3.
For some reason, when I try to map it, field1 comes OK, Field2 gives me an error and field_3 is okay as well.
Field2 has been mapped as follows:
public virtual DbSet<field2> field2 { get; set; }
Manually editing it like this works and fixes the problem altogether:
public virtual DbSet<Field2> field2 { get; set; }
However I don't know why EF fails to build it correctly, since it's technically reading from the database and there's no other issue related to that. This also belongs to the auto-generated file that contains entities to work with, so I'm a bit afraid my changes might break something, on top of the file being automatically regenerated every time someone updates the model from the database.
Thanks in advance.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace MyProject.Models
{
using System;
using System.Collections.Generic;
public partial class Field2
{
public int IdFoo { get; set; }
public int IdBar { get; set; }
public string Name { get; set; }
public Nullable<int> IdSomething { get; set; }
public Nullable<int> Idwhatever { get; set; }
public string Blah { get; set; }
public Nullable<int> idBleh { get; set; }
}
}
Upvotes: 0
Views: 717
Reputation: 494
I found the answer. Turns out EF6 is plagued with errors including this one.
For this reason, they stopped supporting Database-first models and incentivated Code-first approaches instead of auto-generated classes.
In short, move on to EF Core if you can and use Code-first as your go-to procedure.
Upvotes: 1
Reputation: 23983
public virtual DbSet<field2> field2 { get; set; }
In your above code the first field2
referenced there is a Type
while the second is a property / variable
(see this link for the difference between variable
and Type
).
The reason that:
public virtual DbSet<field2> field2 { get; set; }
doesn't compile is that you don't have a class called field2
in your codebase.
The reason that:
public virtual DbSet<Field2> field2 { get; set; }
does compile is that you do have a class called Field2
in your codebase. The typename is case sensitive.
Upvotes: 0