I have considered many other questions on SO, but I cannot get an answer. I have a column in the table: Sex - Male
I would like to access those who named it, as this gives me problems with EF. If I use this:
[Column("Sex - Male")] public bool Sex { get;set; }
This gives me the error of being incompatible with the model, because the Sex field cannot be found. So I changed this:
[Column("[Sex - Male]")] public bool Sex { get;set; }
Then I get the message Invalid Column Name [Sex - Male]. Does EF rename columns with spaces somehow since this field exists and is not some kind of FK?
EDIT
I found this to be done in modelBuilder:
modelBuilder.Entity<Student>().Property(x => x.Sex).HasColumnName("Sex - Male");
It causes the same error, which says that it is incompatible, because there is no column called "Sex with the same name! I noticed that this happens in everything that I use the annotation of the column data not only for this field!
EDIT 2
I created a new application and used Model Designer to see how he interpreted the column and showed it in the designer as โSex___Maleโ, however changing the class before even using [] around it still allows me not to find the column Paul ___ ??
EDIT 3
It seems that the error is not exactly what I thought, I found that the mapping configuration works fine when I just use db.Students; and the column is there as expected. Turns out the wrong area is the line:
var students = (db as IObjectContextAdapter).ObjectContext.ExecuteStoreQuery<Student>(sql);
It is therefore clear that ExecuteStoreQuery, which I assume will not use the same display configuration, therefore sees that the column is missing. Not sure why posting a column annotation on a property in a class does not work though ??
c # entity-framework
user1166905
source share