View Ticket
Not logged in
Ticket UUID: c61ae5a66977d4a017671292fc4eb1d713207a39
Title: Code first from database not set the primary key
Status: Closed Type: Incident
Severity: Important Priority: Medium
Subsystem: LINQ Resolution: External
Last Modified: 2016-01-14 17:34:05
Version Found In:
User Comments:
anonymous added on 2016-01-03 19:48:51:
Until now, I have used the edmx created from database first, and in the emdx model I can see that the field that is primary key, is set as primary key.

However, when I use code first from database, in the method OnModelCreating the relationships are created, but the primary keys are not set, so I get an error when I try to run my application.

Why when I use the edmx the primary keys are setted but not in code first from database? It would possible that you added this opton?

Thank you so much.

mistachkin added on 2016-01-03 21:38:42:
Are you using Entity Framework?  Which version?  What are the steps you followed
to setup the code first project?

anonymous added on 2016-01-07 18:12:06:
I am using VS2015 community x64 and I have installed the last version of Sqlite ( to install the dsign components of Sqlite. I am using Windows 10 pro x64.

These are the steps that I am following:

- Install EF 6.1.3 thourh nagets manager in the project. I do that because if not, when I install Sqlte from nugets, it installs the version 6.0.0.

- Later I install Sqlte from nugets manager. It detects that I have installed EF 6.1.3 so it only installs the Sqlite dlls.

- I add a new item in my project, I select the template data--> ADO.NET entity data model.

- I choose the model, I choose code firt from database.

- I create a new connection, in advanced options, I set foreign keys to true.

- Then I have to select the objects that I want to add. I select all the tables. Only tables. I don't have views nor other objects.

This finishes the process. When I go to the model.cs file, that cointais the class that inherits from dbContext, in the method OnModelCreating, there does not set the proimary key, so when I will go to access to the database, I get an error that says taht the entity has not set a primary key.

I hope that this information can you help you.

Thank you so much for your work.

mistachkin added on 2016-01-08 00:20:39:
Are you able to share the schema of the table that has the issue?

anonymous added on 2016-01-08 17:51:44:
I have this table:

CREATE TABLE [Almacenamientos] (
  [IDTipoAlmacenamiento] INTEGER NOT NULL CONSTRAINT [FK_Almacenamientos_IDTipoAlmacenamiento] REFERENCES [TiposAlmacenamientos]([IDTipoAlmacenamiento]), 
  [NumeroSerie] VARCHAR, 
  [EspacioTotal] DECIMAL, 
  [IDUnidadesCapacidadEspacioTotal] INTEGER NOT NULL CONSTRAINT [FK_Almacenamientos_IDUnidadesCapacidad] REFERENCES [UnidadesCapacidad]([IDUnidadesCapacidad]), 
  [EspacioDisponible] DECIMAL, 
  [IDUnidadesCapacidadEspacioDisponible] INTEGER NOT NULL);

CREATE INDEX [IX_Almacenamientos_IDTipoAlmacenamiento] ON [Almacenamientos] ([IDTipoAlmacenamiento]);

CREATE INDEX [IX_Almacenamientos_IDUnidadesCapacidad] ON [Almacenamientos] ([IDUnidadesCapacidadEspacioTotal]);

How can yo see, I have column which name IDAlmacenamiento that is a primary key not null autonumeric.

The fluent code that generates the code first from database is this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
                .Property(e => e.Nombre)

                .Property(e => e.NumeroSerie)

                .Property(e => e.EspacioTotal)
                .HasPrecision(53, 0);

                .Property(e => e.EspacioDisponible)
                .HasPrecision(53, 0);

How can you see, the is not set the primary key.

Thank you so much.

mistachkin added on 2016-01-09 00:11:47:
Was the code in OnModelCreating generated by something or was it written by you?

anonymous added on 2016-01-09 08:27:52:
All the code is autogenerated, I mean, I have not written any code.

anonymous added on 2016-01-09 08:30:00:
I forget to say, that I don't know what generates this code, if Sqlite or Visual Studio or EF, I just use the wizard to select the connection, select the tables that I want and I select the option of code frist from database.


mistachkin added on 2016-01-13 00:19:18:
It looks like the Code First stuff is meant to be customized:


Also see "Custom Code First Conventions (EF6 onwards)":


anonymous added on 2016-01-13 09:21:26:
Well, I am reading the documents and if I am not wrong, there is two options. Follow name conventions or add in the OnModelingCreating method the configuration to each entity.

The first one I would like because I want to use my own name of columns, and the second one makes me to add manually the configuration, so it is not very useful, for that I wouldn't use to databse first, I would use code first.

My doubt is, this is problem of EF or Sqlite? Because when I use edmx it sets the primary key correctly. Why code first from database does not do it?

Thank you so much.

mistachkin added on 2016-01-13 21:37:52:
This issue appears to be caused by a mismatch between the expected conventions
of Entity Framework and the database schema.  At this point, I do not think there
is a way to solve this by changes to System.Data.SQLite.

anonymous added on 2016-01-14 08:34:13: