Entity Framework Core - Computed Column issue - entity-framework

I have an entity class with a column called 'CalculatedId'. This column is obtained from an SQL statement and the column does NOT exist in the database table.
If I run app I get an error telling me that the column does not exist. This error is due to the framework trying to map the column to a physical column.
Below is my mapping code:
modelBuilder
.Entity<Widgets>()
.Property(x => x.CalculatedId)
.HasComputedColumnSql("select top 1 Id from MyTable");
I thought that this would tell EF not to look for a physical column as it's generated.
The only way I can get rid of the error is if I do this:
modelBuilder
.Entity<Widgets>()
.Ignore(x => x.CalculatedId);
Problem there is that the column is totally ignored an not generated at all.
Can someone please connect the dots for me and clarify what needs to be done to make sure that the column is generated but EF does not look for it in the database.
NB: Has to be done in the fluent API, no data annotations.

Related

Is it possible to edit a migration in Entity Framework?

I have a database called MembershipType which contains 2 fields.
I added a new field call 'Name'. I wanted to hard-code the data in to the database by using a SQL query.
I wanted to seed data into the column 'Name' which is the other column is already filled with. Is that possible or not?

Why does Entity Framework insist on renaming columns?

Step 1: import code first from existing database. Existing database has a table with the same name as column.
Step 2: in this scenario, Entity Framework sticks a "1" in front of column name in code.
Step 3: when I try to rename it "by getting rid of 1 in front", I get error
member names cannot be the same as their enclosing type
Why is this limitation on EF and is there a solution that doesn't ruin the database in future migrations (by having that column renamed)?
Being forced to use Column1 just seems really terrible and arbitrary.
Thanks.
As pointed out by #shf301 you cant have a property named the same as the class its in, this is a .NET restriction.
However you can name your column anything else and then tell EF to point to your specific column in the database
eg:
[Column("MyColumn")] // "MyColumn" will be what EF expects in the db
public int FlyingMonkies {get;set;}

If I use EF Code First then how can I add another column to a table?

I created POCO classes and then EF created the database tables for me when I tried to access the data. This worked without problem. I have now populated my tables with data. Not just seed data but real data.
Now I would like to add another column to a table. I assume the first thing I need to do is to add a field to the POCO class but what's next after that? I now have my database filled with data. On the SQL side I know how to add the column myself but do I have to do something with EF or will it automatically pick up that my column was added to the table and my field to the POCO class?
You can use Code First Migrations (http://msdn.microsoft.com/en-us/library/hh770484(v=vs.103).aspx). It can update your database automatically or not.

EntityFramework inheritance - Ignore not nullable column

I have an one entity in my edmx model having an one property that can contains huge XML data.
Basically I want to load this entity without this property (column) /* huge data loading */ . And load this column only when it is strictly needed.
I have tried to create an inherited entity containing this property and remove this property from base entity (original entity). I have done mapping.
At this time I have problem, that during compilation a I get error, that base entity is not capable to insert and update itself, because property is not nullable
I am looking for best approach (solution) how this situation should be solved.
I am attaching the cut-out from my emdx designer (containing my current and desired situation)
UPDATE:
I will try to write a procedure that I have tried:
I mapped functions to my custom functions. For entity TRP_TechReport_T without the XML column (property). Then I just mapped for entity TRP_TechReport_T functions to my custom function (containing XML column).
Then I set Mapping condition on the entity TRP_TechReport_T: When TRP_XML = Empty.String
TechReport_T mappings:
TechReport_T functions:
TechReportFull_T mappings:
TechReportFull_T functions:
At this moment I get error:
Error 2 Error 3032: Problem in mapping fragments starting at line 3754:Condition member 'TRP_TechReport_T.TRP_XML' with a condition other than 'IsNull=False' is mapped. Either remove the condition on TRP_TechReport_T.TRP_XML or remove it from the mapping.
The column is not nullable in the database and mustn't be.
I can hard-set XML property to nullable, but in the case of the model updating from the database information will be lost.
At the moment it's the only thing I could think of.

How to affect the column order with Entity Framework Code First Migrations

I'm using Entity Framework 4.3 Code First and trying out the Migrations feature.
If I add a new property to my class and then run Add-Migration from the package manager console window I get something like this:
public override void Up()
{
AddColumn("Products", "Discontinued", c => c.Boolean(nullable: false));
}
I would like to be able to affect the order of the column as I don't want it to just be appended to the table but rather placed at a specific index. I thought I might be able to add it to my modelBuilder configuration, something like:
Property(p => p.Discontinued).HasColumnOrder(2);
but running Update-database does not appear to use it. Can this be done as a migration?
This is just a matter of missing functionality. SQL by itself does not rely on any implicit order of columns (with some exceptions: ORDER BY , ...).
Neither SQL Server nor ORACLE do have a direct SQL DDL command (aka ALTER TABLE...) to move a column around.
Therefore there's no possibility to change the order without high effort (recreate the table). See for example
How To change the column order of An Existing Table in SQL Server 2008
SQL SERVER – Change Order of Column In Database Tables
https://dba.stackexchange.com/questions/61978/how-to-change-the-column-order