EF does not generate every model from database using SQLite - entity-framework

I am trying to change the service based database in my project to SQLite. I am using EF database first approach but the EF does not generate every model from database. Eventhough foreign keys are set it does not generate the connections and also returns an error:
> Error 6005: The data type '' is currently not supported for the target Entity Framework version; the column 'Id' in the table 'main.Comments' was excluded.
It does so with every table that has an Id column (integer, primary key).
How can I fix this?
Thanks

Related

Adding a primary key into an existing table using Entity Framework Core

I have an existing, populated table. Using an EF Core migration, I want to create a DB Generated Primary Key.
Here's my fluent API:
modelBuilder.Entity<MyTable>()
.HasKey(a => a.Id);
modelBuilder.Entity<MyTable>()
.Property(p => p.Id)
.ValueGeneratedOnAdd();
The issue that I have is that, when I run migration, I get an error, because the existing rows in the table do not have this (new) field populated:
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.MyTable' and the index name 'PK_MyTable'. The duplicate key value is ().
Is there a way that I can leverage the default value to retrospectively populate this table with primary keys? Alternatively, is there another way to populate this field retrospectively?
Alternatively, is there another way to populate this field retrospectively?
Yes. But not through EF. You'll need to clean up the database manually before EF can start managing the schema.

Entity Framework Code First Migration and Data Migrations

We are using Entity Framework Code First with Migrations. We have it running without any issues. We are trying to migrate data from an old database to the new. We need to drop the database, create all the tables, insert the data and then add in the primary keys and foreign keys. We want to do this in EF so that we can format the data being migrated. We have been successful in creating the database and then migrating the data over but the primary keys are in the old database are not coming over. I've tried using the ExecuteSqlCommand before a bulkinsert:
ctx.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[TableName] ON");
but this does not work because the migration scipts have the idenity set to true:
Id = c.Int(nullable: false, identity: true),
Is there a way to set the identity to false then after the data is inserted into the database, set the identity to true?
You are doing it in the right way (Id is an identity and you enable identity insert). SET IDENTITY_INSERT ON is valid only for the session (the connection) so you need to use only one connection.
Also, you have to seed the database using INSERT statements (and not EF SaveChanges).

Firebird primary keys and EF6 store generated pattern

in my database first application (Firebird), the primary keys are not set to identity by default !
how to fix that from the t4 template generation file ?
thanks and good day
Primary keys are not necessarily identity columns, and Firebird 2.5 and earlier doesn't have identity columns. Instead you simulate it with a trigger and a sequence/generator, but this isn't 'detectable' from a metadata perspective (or at least pretty hard to infer correctly). Identity columns will be introduced in Firebird 3.
For the entity framework client for Firebird to recognize the column as identity, you need to add a comment to the column (in the database!) with the text #PK_GEN#, like so:
comment on column yourtable.yourcolumn is '#PK_GEN#'
See also: Generated primary key in Entity Framework model from Firebird

When I add a column in the database, under what conditions do I need to update my EDMX?

When I add a column in the database, under what conditions do I need to update my EDMX?
To elaborate:
I know if I add a non-nullable field, I need to update the model if I want to write to the database. What if just I want to read?
What if it's a nullable field? Can I both read and write?
What if I were to change the primary key to the new column but the edmx still has the old column as primary?
1) If you want to port an old database, you need to make sure that every table in your database must have a primary key. This is the only requirement for creating the EDMX.
2) If you've added a column in a table at database side, and have not updated edmx, then you'll simply not be able to use that column though EntityFramework.
If you create a non nullable column with no default value, the insert operation will fail with exception "Cannot insert null into column , statement terminated". And the you'll not be able to read values of that column using entityframeowrk, unless you update the edmx.
3) If you've changed the primary key of any table at database side, and if the edmx is not aware of that, your application might create a runtime exception when performing operations with that table.
Remember, Entity Framework creates SQL queries depending upon its knowledge of database(which is defined in EDMX). So if EDMX is incorrect, the resulting SQL queries so generated might lead to problems at runtime.

Entity Framework Generate Database Schema (SQL) with Default Table Values

I am using EF 5 and SQL Server 2005, Model First (sort of).
By sort of, I mean that I typically build my schema in the SQL Server designer, but import the schema into EF so I have a visual view. There is often round-tripping.
However, I noticed that when I try to generate the DB schema based on the EF model, it skips all of the NEWID() default values that I have assigned as default values to my Guid IDs, but it doesn't skip the identity fields of type int.
I found this post explaining the reasoning for this:
Entity Framework 4 and Default Values
However, it doesn't answer my question: How do I get Entity Framework to generate a SQL DDL database schema with default values of NEWID() for my uniqueidentifier types?
NOTE:
I don't care about how to set them from the POCO entities and so forth (there are plenty of posts describing that) - my concern is getting the SQL DDL generated right so I can seed the database without worrying about these values going missing.
Using Entity Framework Migrations, you can use the GUID column builder and its DefaultValueSql parameter. The value of that parameter can be the string "NEWID()". This should take care of proper DDL generation.
Next you should declare these properties as database-generated using attributes or the fluent model builder, so that EF ignores the values set in your POCOs (which will be null for new objects).