INSERT fails on EF 4.1 Code First Database - asp.net-mvc-3

I am using Visual Studio 2010 SP1 with MVC3 Tools Update, and EF 4.1 Code First to scaffold my application's database.
Database CRUD operations work fine when testing locally and on a local database. It is not until I migrate the database to web host's SQL Server 2008 R2 and target that hosted database in my connection string that all INSERT operations fail.
Whenever I try to add a new record I get this error: [I]Cannot insert the value NULL into column 'Id', table 'DB_23378_bloomlmsdata.dbo.Courses'; column does not allow nulls. INSERT fails. The statement has been terminated.[/I]
My data models all specify a Primary Identity Key like so:
[Key]
public int Id {get; set;}
...
And in the local database that gets scaffolded, I see a non-nullable Primary Identity Key in each table.
I read somewhere that Entity Framework, when performing an INSERT operation to the database it will first try to insert a record with a null value as the Primary Key. I do not know how to override this behaviour. Also, this does not seem to be a common problem and people usually only run in to this when they use something different than an IDENTITY key. For the record, I had the same problem with EF CodeFirst CTP5 before EF 4.1 was released.
I have been troubleshooting this for 4 weeks.. My web host is up to date with the technology I am using in my application and database. When I try to get help, they are telling me it is a coding issue but my code checks out. Any ideas would be greatly appreciated.

Check the IsIdentity property of the Id column, it should be set to yes. EF doesn't pass a value for the identity column on an insert because it should be auto-generated by the database.

Related

EF Core 2.0 Invalid Column Error from ASP.NET Core UserManager

Have started new ASP.NET Core 2.0 and EF Core 2.0 project. The project uses the built in Identity table schema. Every time I make any call to the built in UserManager that uses any of the Roles or Claims tables the call fails with a "Invalid Column" error. I added Serilog so I could see all logging and EF Core generated SQL. EF Core is adding the same invalid column name "BlogUserId" to all the select queries that are failing thus the invalid column error. That string does not exist in the code anywhere including the EF Core Migrations code that generates the database and tables. Where is Entity Framework picking up this column?
SELECT TOP(1) [e].[UserId], [e].[RoleId], [e].[BlogUserId]
FROM [AspNetUserRoles] AS [e]
WHERE ([e].[UserId] = #__get_Item_0) AND ([e].[RoleId] = #__get_Item_1)
System.Data.SqlClient.SqlException (0x80131904):
Invalid column name 'BlogUserId'.
You can see where the column gets added to the generated query but that column is not part of the table schema nor is it in code anywhere. How do I fix this issue or can someone tell me what is causing this?
Thanks to Ivan Stoev the solution was found. The BlogUser class was a carry over from the old web site which was ASP.NET Core 1.1. It had additional properties but also had two navigation properties as below to store user Roles and Claims which had worked perfectly. After commenting these to navigation properties every thing seems to work as expected.
public virtual ICollection<IdentityUserRole<string>> Roles { get; } = new List<IdentityUserRole<string>>();
public virtual ICollection<IdentityUserClaim<string>> Claims { get; } = new List<IdentityUserClaim<string>>();

entity framework error after attached .mdf file

I attached an .mdf file in SQL Server 2008 and used that database for my entity framework database first project. Below is the error I got
Exception Details: System.Data.MappingException: Schema specified is not valid.
Error 2062: No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer"
I tried this link: How do I correctly set an association between two objects in the Entity Framework 4 Entitydesigner?
but it did not work for me. can anyone help me what can be the problem.
thanks,
michaeld
This exception usually occures if you have an entity in your model which doesn't mapped to a table (or an object) into the Database.
If you want that your project just starts debugging, Remove all entities from your model, Right-click in model designer and choose Update model from database
If you have an entity which it supposed to be mapped to a table into database, you should create a relative table in database, and map your entity to that table. You also can ghange your approach from db-first to code-first and enable migrations so that EF updates your db according to your model.
If you have an entity and you want to map it to a stored procedure in your db, see here

EntityKey properties of type “Geography” not supported. Entity Framework 5, VS 2012 – Database First

Created a SQL Server 2008 database and table with a property called “Coordinates” of type “geography”. In VS 2012 created a project, added references to Microsoft.SqlServer.Types and EF V5. Then, using the ADO.NET Entity Data Model wizard generated the edmx. When I compile in VS 2012 I get the following error:
Error 4 Error 129: The property 'Coordinates' in EntityType 'DBModel.GeoDestination' is not valid. EntityKey properties that are of type 'Geography' are currently not supported.
Many of the samples on the web are “code first” examples that use the type “DbGeography” as a type in their c# code and generate the database from the code resulting in a table with a property of type “geography”.
How do I do this database first and fix up the type? It does not appear that “DBGeography” is in the drop down list of types when using the “Table Mapping” view.
Using EF 5, NET 4.5, VS 2012 SP 1, SQL Server Types
I figured this out. Looks like a bug in the wizard. If the table in SQL does not have a primary key, the wizard automatically generates a key(s) in the resulting EF model. It appears random, sometimes one key is generated and sometimes it makes several properties keys. A Geography data type cannot be a key. It was not a key in the table, but the wizard made it a key.
Hope this response helps others.

InnerException {"Invalid object name 'dbo.Users'"}

I am currently trying to setup an ASP.Net MVC 3 Application for the first time. I know what I am doing with Ruby on Rails but completely lost with ASP.Net. I would like the app to create the database but not sure what I am doing wrong. I have an empty Database build called ssDB. I have setup the connectionStrings and built out the Model classes. According to everything I have read thus for, when I go to the run the App it should create the database but it doesn't look that way.
The Abbreviated Error I get is:
InnerException: System.Data.SqlClient.SqlException
Message=Invalid object name 'dbo.Users'.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=208
Procedure=""
Server=TESTSVR01
State=1
I have an empty Database build called ssDB
You have created an empty database with no tables. Since there is a database EF assumes tables are also created. Hence it will not proceed to create the tables.
You can drop the database and allow EF to create the database and the tables.
I also experienced the same issue while using Database first approach. Connection string in my service's/service's host web.config was incorrect. I corrected the connection string and it worked.

Database migrations in Grails

Suppose I have a database containing 3 tables in a grails application:
User
Activity
Friend
User table table has one to many relation to Activity and Friend tables so in User table I have:
static hasMany = [activies: Activity, friends: Friend]
and in Friend and Activity I have:
static belongsTo User.
Application is deployed and lets says thousands of customers have registered. Now changes are made in the database: Table Activity is dropped. A table Journal is created which is on the many sides of the User table. The User table has a new column added and this column cannot be null. An old column in Friend table is deleted that was also defined as not null.
So given above scenario and assume using MySQL what needs to be done to make above changes without deleting existing customers data and safely add the new table to existing customers?
Ruby on Rails comes with ActiveRecord for database migrations. Does Grails comes with something like this out of the box?
Currently in development when I run my grails application after adding a new not null column to a table, I get column cannot be null exception thrown unless I delete that table in the database before running grails application which would recreate the table if not exists. Obviously once the application is deployed I will not have the luxury to delete the table.
Unfortunately, the current version of Grails doesn't come with database migration. However, there is a plugin for Liquibase which makes migrations possible.
The next version of Grails (1.4, planned for Q1 2011) will supposedly contain a built-in migration tool, which I am very much looking forward to.
Note: I haven't used the Liquibase plugin, so I don't have any firsthand experience with it. I have seen numerous blog posts describing its use, however, and I'm probably going to use it in my next Grails project if 1.4 isn't out by then.

Resources