When a School entity is added to the database, the discriminator is being set to a guid instead of "school". Subsequent reads where the type is School does not return the new row. Why is it a Guid? I don't define/set the discriminator in my code.
Using EF Core 1.1.2 with VS 2017 15.2 with SQL 2016.
Thanks for your response and sorry for taking so long to respond. The project is rather large and difficult to isolate the issue.
The solution was to add Microsoft.EntityFrameworkCore.SQLServer version 1.1.1 to my project. Prior to that, the project had no reference to it.
When the project was inserting a guid for a discriminator, I could VS => Debug => Windows => Modules and see that Microsoft.EntityFrameworkCore.SqlServer.dll was version 1.00.4.30428 (presumably from a child project).
When the project was inserting the class name for a discriminator (my desired behavior), I could VS => Debug => Windows => Modules and see that Microsoft.EntityFrameworkCore.SqlServer.dll was version 1.01.1.30217.
Thanks again.
Related
I have an existing EF code first project which has created the database with all nvarchar columns in the database. Now I am starting another project on the same database for statistical purpose. This new project however is using EF core as pointing to same database. When I tried to run the new project, it gives me following error.
"Data type 'VARCHAR' is not supported in this form. Either specify the length explicitly in the type name, for example as 'VARCHAR(16)', or remove the data type and use APIs such as HasMaxLength to allow EF choose the data type."
Now, as I already have production data in the database so I want to make minimum impact to the column types but still wan to use EC core in my new project. I have so many nvarchar columns so setting configuration on individual table is a hard job. Anyone can point me in the right direction?
This seems to be an issue with the 2.0 release of EntityFrameworkCore: https://github.com/aspnet/EntityFrameworkCore/issues/9188
Supposed to get fixed in 2.1 so you may want to wait until then.
Otherwise they suggest manually fixing it like this:
entity.Property(e => e.Comments)
.HasColumnName("Comments")
.HasColumnType("nvarchar(4000)"); // <- Add this
I'm using data annotation and it still works.
[Column(TypeName = "varchar(50)")]
I can confirm its been fixed on later version, not sure about 2.1 but it disappeared in 2.2 for sure.
https://github.com/aspnet/EntityFrameworkCore/issues/13609
Using Visual Studio 2015 with SQL Server 2012. I've been happily using code first existing database to generate my data layer with no problems, until last week. For some inexplicable reason VS will not generate the Entity classes from my database tables (any solution, any projects, different databases and SQL instances).
The DbContext inheriting model class is generated containing the public DbSet properties for each table, also the OnModelCreating method which contains an entity<>.Property assignment for each field in each table.
Basically VS can talk to the database, read the schema, map and create everything except the entity classes. I am working on Win Server 2008R2 and applied a number of updates prior to the issue. I have since removed the updates, uninstalled then reinstalled VS but to no avail. Searching the web does not highlight anyone else who has encountered this. Can anyone help please? (VS 2013 is working fine).
I've been using Entity Framework Code First migrations for a while now, and like to think I know the fundamentals pretty well. However, yesterday I came across a rather odd issue which I can't seem to find a solution to.
I've added a reference to a DLL (third party - not developed by ourselves) which has some interfaces I need on my entities. The DLL contains a bunch of other things - such as some classes - which we don't need and aren't using.
Now with the reference, and implementing some of the interfaces - when I add a migration using the Add-Migration command, Entity Framework seems to pick up some of the classes in the DLL, and tries to create tables for them in the migration.
For example, I have a 'User' entity in my project. I have added a reference to ThirdParty.dll, which contains an IThirdPartyUser interface. The DLL also contains a UnusedClass, which I am not using - it is not part of my context in any way.
When I create a migration, I get code to create the database tables for UnusedClass. For example:
CreateTable(
"dbo.UnusedClass",
c => new
{
Id = c.Int(nullable: false, identity: true),
RegistrationNumber = c.Int(),
})
Now my theory is that this external assembly (incorrectly) has some dependency on Entity Framework - but I can't figure out why migrations is trying to generate database code for them - especially as they are not part of my context.
Anyone come across this before? I'm about to embark on a trial and error exercise to try and get to the bottom of the issue.
EF should only pick up classes that are in the object graph originating at your DbContext - so, if class A is in your context, and class A has a property of type UnusedClass, EF will try to scaffold it.
You can add the NotMappedAttribute to a property or class to have EF ignore it when generating its mapping.
My application is using a database first EDMX in EF 4. I would like to upgrade everything to EF 6. After getting EF 6 with NuGet I had to make a lot of changes to my classes that are using my EF model, because namespaces have been changed in EF 6. Then I realized, that the code generated by my EDMX does also use the wrong namespaces etc. I'm not using a custom T4 so far.
How would I upgrade my existing EDMX to EF 6.
Thank you.
You delete your old .tt files
You open your edmx file in designer mode (so you can see your model)
Right click on a free space
Select Add Code Generation Item
In the dialog select "EF 6.x DbContext Code Generation Item" (something like this)
Save your edmx and all classes will be generated for you, with the new namespaces and so on
In addition to the answers given here by Rand Random and Dean Oliver, let me mention the following MSDN link, describing general steps for upgrading to EF6. Don't underestimate the manual steps required...
The road map is (see details in the link given above):
Preparation: Install the Entity Framework 6 Tools for Visual Studio 2012/13
Install the EF6 NuGet package
Ensure that assembly references to System.Data.Entity.dll are removed (Note: Installing the EF6 NuGet package should automatically remove any references to System.Data.Entity from your project for you).
Swap any EF Designer (EDMX) models to use EF 6.x code generation. Notes:
If you're getting the message "The Entity Data Model Designer is unable to display the file you requested" afterwards, then click on the link modify in the displayed text message "The entity Data Model Designer ... You can modify ...", which will display the tables. Select all with Ctrl+A, then press Del, then right-click and select "Update model from database", and finally save using Ctrl+S. This will update the model to the latest version using the default T4-Template "EF 6.x DbContext Generator".
If you have used ObjectContext in your project, then you should consider downloading the template "EF 6.x EntityObject Generator". Then right-click in the model designer, choose "Add code generation item", then choose a name you haven't used yet. It will generate the right classes, afterwards you have to remove all old ("*.tt") files and related generated class ("*.cs") files.
Update namespaces for any core EF types being used, i.e.
any type in System.Data.* is moved to System.Data.Entity.Core.*
System.Data.EntityState => System.Data.Entity.EntityState
System.Data.Objects.DataClasses.EdmFunctionAttribute => System.Data.Entity.DbFunctionAttribute.
Note: This class has been renamed; a class with the old name still exists and works, but it is now marked as obsolete.
System.Data.Objects.EntityFunctions => System.Data.Entity.DbFunctions.
Note: This class has been renamed; a class with the old name still exists and works, but it is now marked as obsolete.
Spatial classes (e.g. DbGeography, DbGeometry) have moved from System.Data.Spatial => System.Data.Entity.Spatial
N.B.:
More information about available EF templates can be found here at MSDN.
If you're getting an obsolete attribute warning after upgrading to EF6.x, check out this SO article: How to get rid of obsolete attribute warning?
As well as the steps Rand Random suggested. Remember to Install Entity Framework 6 Tools for Visual Studio 2012 if you are using VS2012. download here
This will ensure that EF 6.x DbContext Generator template shows when clicking Step 4: Add Code Generation Item
I developed a Repository component targeting the .NET 4 Framework that uses EF. In my EF I use tables already existing in the Database.
In the Database I have 4 tables representing entities and 3 association tables (the relationships between the entities are all many-to-many)
However these tables at Database level do not have any relation between each other. Since I do not have rights to modify the Database, I added the relations directly in the EF. I named them, I specified the navigation properties and then I successfully compiled and tested the project.
Now I have to reference this Repository project from a Web application using the .NET 3.5. Therefore also the Repository project must reference the .NET 3.5.
I correctly copied the connection string from the Repository project to the web.config, I compiled the project and...it gives me a compile error saying that there is
"no mapping specified between EntitySet/AssociationSet"
and then lists the relationships between tables/entities.
I guess this error is given because there are no associations at Database level. How can I solve this issue without having to rewrite all my code?
You cannot use an EF 4 EDMX in EF 3.5 period. You can, however, use an EF 3.5 EDMX in EF 4, but the EF 4 designer won't understand it.
One possible fix is to make your EF 4 project an out of process server.