Firebird primary keys and EF6 store generated pattern - entity-framework

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

Related

EF does not generate every model from database using SQLite

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

Does Entity Framework Core have an option to use a default value of 0 instead of null for non existent references?

If I have an object called Project that has a property called Creator and creator is a complex type, EF will automatically use the value null if the Creator property has not been assigned. Is it possible to instead use 0 instead of null in the database fields?
No, that would break Foreign Key Constraints on the database side, since there wouldn't be a Creator row with a primary key of 0 for the Project row's foreign key to point to.
In a relational database, at least those that respect foreign key constraints, every relationship between rows is represented as a pair of primary, and foreign keys. The database is designed to enforce that a foreign key always points to a valid primary key. It will prevent you from updating a FK field to a value that doesn't exist in the PK field. It will also yell at you for trying to delete the row that contains the PK as long as the FK still points to it (unless cascade-delete is turned on, but that gets complicated).
In theory, Entity Framework could probably be forced in to trying to do what you want, but the database would reject it, and EF would almost certainly have issues trying to retrieve rows with the 0/null value in it if it is configured to include navigation properties.

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).

Entity Framework puts all fields in primary key for Firebird tables

I am using a Firebird 2.1 database together with VS2010 (.NET 4.0) and am trying to get it to work properly with the entity framework.
The problem is, that when I generate an entity from a database table, the framework detects all columns to be part of the primary key. The table is very simple with two integer columns, one of them being set as primary key.
I even have "#PK_GEN#" set as comment of the primary key column.
In the EF-Editor I cannot modify the primary key property of the store object, and since I will have to deal with nullable columns, that is a problem. I can edit the XML code of the model file, but the changes are non-persistent when updating the model, so that is a show-stopper.
Since I only read about similar problems concerning views not tables, I am obviously doing something wrong, but I can't figure it out.
edit: By the way, I just tested the behavior with VS 2012 and it remains unchanged.
Here's the CREATE script. Since I'm new to Firebird, there might me something wrong here as well, but I really don't think so.
CREATE GENERATOR GEN_TESTTABLE_ID;
CREATE TABLE TESTTABLE (
TESTTABLE_ID INTEGER NOT NULL,
VALUE INTEGER
);
ALTER TABLE TESTTABLE ADD CONSTRAINT PK_TESTTABLE PRIMARY KEY (TESTTABLE_ID);
COMMENT ON COLUMN TESTTABLE.TESTTABLE_ID IS '#PK_GEN#';
SET TERM ^ ;
CREATE OR ALTER TRIGGER TESTTABLE_BI_GEN_ID FOR TESTTABLE
ACTIVE BEFORE INSERT POSITION 0
AS
begin
if ((new.testtable_id is null) or (new.testtable_id = 0) ) then
begin
new.testtable_id = gen_id(gen_testtable_id, 1);
end
end
^
SET TERM ; ^
Problem is, that Firebird 2.1 contains a bug, that results in this. Generate the model using Firebird 2.5 and you'll be fine.
Some references here, here, here.