MVC 3 Entity Framework - new database column not populating corresponding property - sql-server

I am creating a web app in MVC 3 using entity framework, and recent spec changes have required I add a new column to one of the database tables (a record creation date).
I have added the new column and populated it with values for my existing records, and updated the model in the entity framework. When I look at the object corresponding to the table with the new column, I can see a property for this new data: however the data for that property is not being retrieved.
Just to clarify:
I added column "CreatedDate" (datetime) to table "Orders" in my database
I populated this column with values for my existing records and set it to "not null"
I updated the model in entity framework
My model "Order" now has a non-nullable property "CreatedDate"
I populate a list of orders using LINQ ("from o in DB.Orders select o")
For any entry on my list, entry.CreatedDate returns "01/01/0001 00:00:00", not the value I have entered into the database.
What's going wrong?

EdmMetadata table?
Do you have an EdmMetadata table in the database? As far as I know, this table contains a key that is signed by the structure of the database. So if this value is not changed, the code could not be seeing the change. Good news is that this table is no longer needed:
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

Related

Can we retrieve the field names from the models.py based on the column names in table from the database in a django project?

models.py: class Name(models.Model): name=models.TextField(db_column="Name_of_the_persion")
like this i defined the model, so that table 'Name' will be created on the database side, with column name as "Name_of_the_persion".
My requirement is like,I need to insert the new row in the table based on the django model field names instead of columns of tabel.
If any one knows, pls help me.
I tried inserting the data into table, by using psycopg2. But this was work in the case of pgadmin point of view only, that means its takes column names of database, instead of model fileds .

Change Mapped table dynamically in Entity Framework

I am using Entity Framework. In my DB, I have a table (DOCMASTER) which is mapped in my model, and on occasion, has a backup from a different date created (e.g. DOCMASTER_10_01_2015). The mappings are identical, with the exception of the keys/constraints. These are not brought over with the backup.
I have an application with a dropdown that is filled with all of the tables in the DB that are of type "DOCMASTER". The user selects which table they would like to query from, and they search for a client in that particular version of the table.
What I would ideally like to do is remap my model to use the selected table instead of the mapped table, however when I do that using DbModelBuilder, it seems to want to remap all of the tables in that model, not just the one table. I receive the error "CodeFirstNamespace.CUSTOM1: : EntityType 'CUSTOM1' has no key defined. Define the key for this EntityType" wherein 'CUSTOM1' is my table name, but I receive this for all of my tables in the model.
I am debating just using a paramaterized query to query the selected table directly. Does anyone have any thoughts on this?

Database "Identity" field and auto-increment

I'm writing my first database application and I've got an ambiguity I can't seem to find an answer for. I have an id field that is the identity set to auto-increment. My issue is trying to determine when the field is incremented. Is the field incremented when I call an instance of the object, when I call the AddObject method of the ObjectContext class, or when I call the SaveChanges method from an Entity model.
In my relational database each table has both a unique ID for that table and one that represents a group of users. After I create an instance of an object for that table I want to run a query (LINQ) that searches two tables to match two records and from one of those tables copy that group ID back to the individual user.
That or it is blatently obvious I know nothing about how relational databases work,
The identity field is handled by the database. It is created by the database when the row is inserted. The generated id is read back by SaveChanges and the entity object is updated.
WHen you add a new row to the database the counter is incremented.
If you have an ambiguity, this usually means that two tables fields are the same name, and your query doesn't know which one you want. Can be solved by defning which table the column is ment for.
I don't know LINQ, so hopefully someone can give you a more direct answer.

Why doesn't the Entity Data Model Wizard produce an entity reflecting a db table with a default value of getdate()?

I have a database table where one of its columns is of type datetime. The default value for this column is set to getdate().
Using EDM wizard, I generate the entity model. Unfortunately, the generated entity model class does not set the column of a new row to the date on which the row is created.
Why doesn't the EDM Wizard produce an entity reflecting a db table with a default value of getdate()?
The "default value" attribute on scalar members in EF models are for constant values only. What you need is setting StoreGeneratedPattern to "Computed" in order to indicate for EF that the column will get a value assigned db-side.

How can I model non-matching Foreign Keys

I'm trying to use EF to model an existing SQL database. The DB is multi-tenant by having a clientID column in every single table (I cannot change this). I have table structure like the following.
Table 'ItemID' columns:
ClientID (pk)
ItemID (pk)
ItemName
Table 'Items' columns:
ClientID (PK)
ItemID (PK) [FK to ItemID.ItemID]
Version (PK)
ItemAttribute1
ItemAttribute2
ItemAttribute3
The DB is designed to store previous versions (rows) of the 'Item' object, hence the 'Version' column and PK.
I am new to the EF and trying to adopt it for my project. However, it seems that EF cannot handle this situation very well. I am open to all ideas including using stored procedures or views instead of access tables directly from EF. What about getting rid of the PKs and using 'independant' relations instead?
One specific problem I ran into is that EF (at least the designer) requires all PKs in one table be mapped to all PK columns in any related table. This does not make sense to me, as the example I've given will never work given that constraint. I am trying to make Items inherit from ItemID. The error I get is:
Error 3003: Problem in mapping
fragments starting at line 170:All the
key properties (ItemID.ClientID,
ItemID.ItemID) of the EntitySet ItemID
must be mapped to all the key
properties (Items.ClientID,
Items.ItemID, Items.Version) of table
Items.
I have been looking up all I can find on this topic and nothing has answered these questions for me. I appreciate any help. Thanks.
The error that you are getting from your EDM is coming from the fact that EF supports inheritance only if both entities have the exact same primary keys (hence a one to one relationship on database) so you cannot have inheritance between these 2 entities according to the current schema.
However, I don't see a problem on having a One to Many association between ItemID and Items entities and I believe this is the default EF behavior when you update your model from the database.
Please have a look at this post for more info:
Entity Framework, TPT Inheritance

Resources