EF Database First - Mapping-error 3025 - entity-framework-4

New to EF and trying something out with "Database first".
Error 3025: ... :Must specify mapping for all key properties
(PurchaseUsers.PurchaseUsersId) of table PurchaseUsers.
I have in my db 3 tables:
Purchases Participants PurchaseUsers
PurchaseId ParticipantId PurchaseUsersId
... ... PurchaseId
ParticipantID
The table PurchaseUsers is to know which participant(s) is(are) using a purchase.
At first I didn't had the PK on that table but then I got the following error when trying to save a Purchase.
After googling a bit I found out that I had to add a PK to avoid this error.
Unable to update the EntitySet 'PurchaseUsers' because it has a DefiningQuery
and no <InsertFunction> element exists in the <ModificationFunctionMapping> element
to support the current operation.
But adding the PK created the mapping-error and I just can't figure out how to solve this or create a mapping.
The table PurchaseUsers itself isn't visible in my .edmx model, but it's listed in the Store in the Model Browser.
Thanks.
UPDATE
Changed the name of a column in the database today. "Update model from database" added the new columnname to the table in the model, but did not remove the old one.
Had to start from scratch once again.
Looks like the update-function is not working very well.

This is weird. Updating the model from the database should make the model and the database to by in-sync. Try deleting and recreating the model from scratch.

Related

Remove entries from a table that maintains a many to many relationship asp.net mvc entity framework

I am writing an application based on an existing database. I have two tables, a server table and a support table (people who support the specified server). These tables can have a many to many relationship, and as such I cannot maintain a foreign key within one of the tables pointing to another.
The solution that the person who designed the schema came up with was to add a third table, a server support junction, that has just two columns - ServerID and SupportID, both foreign keys pointing to their respective table.
When I import this database schema into Entity Framework, it gives me the following warning:
Warning 2 Error 6002: The
table/view 'dbo.Server_Support_Junction' does
not have a primary key defined. The key has been inferred and the definition
was created as a read-only table/view.
As such, the table does not appear in the edmx model and it does not create a class for the table.
As part of the application, I would like the DBA to be able to delete a server or a support (they leave the company/no longer support a certain server/etc). Is entity framework smart enough to see that this table is purely relational and will remove any connections when a support or server is deleted? Or must this be done explicitly?
If it must be done explicitly, what is a workaround for this? I tried adding a primary key called RelationID to the table, but it yelled at me saying that the primary key was not mapped or something.
Gert Arnold helped to find the solution. First, a primary key was added to the table consisting of both the Foreign keys, the SQL was:
ALTER TABLE dbo.Server_Support_Junction
ADD CONSTRAINT pk_ServerSupportJunc PRIMARY KEY (ServerID, SupportID)
I then updated the model by opening the edmx, right clicking on the blank space -> update model from database -> refresh -> finish.
To delete the relationship in the controller, the code was as follows:
Support support = db.Support.Find(id);
support.Servers.Clear();
db.Support.Remove(support);
db.SaveChanges();
Obviously you should do some error checking to make sure the entity was actually found, but that's the gist of it!
Special thanks to Gert Arnold!

Inserting Data to database using Repository in mvc4? unable to insert the multiple data?

I have two Entities billing and BillingDetail
in billing I am adding more than one time and after that I am adding data to BillingDetail finally I am Saving the Database. but when i checked database in billing table, it was containg only last data(only one row that was last updated)
How I supposed to solve this problem?
foreach ()
{
billingRepository.Insert(billModel);
}
billingDetailRepository.Insert(billDetailModel);
dbContext.Save();
When an entity is inserted is will be in attached state. Therefor Entity Framework will know it already exists in the context an not insert it again.
You can detach the entity and then insert it again to have it multiple times. But beware, after an insert PK fields are auto mapped to the entity.

oracle devart entity framework - added New column - now trigger system_id not changing to a unique new number?

oracle 10.4
devart dotConnect - 6.50...
MVC2 project - web page
User fills out form, then controller gets new entity, fills it out. saves database
System_id before saving = 0 (its an int/number - so no cannot be null)
Several other tables linked, so they have their own System_id as well.
When it gets saved to database, some trigger (there is a stored trigger for the table, which I only seam to understand as when system_id=null to be fired), a new Number is assigned for System_id.
This all worked fine.
Then I came along, and needed some updates.
Another field needed on this "main" table
(I have earlier, added columns to another table, with out issue)
Added column to this "main" table (restrict_to_me)
Now when it tries to save to database - it trys saving "system_id=0".
Linked tables, also make records with system_id=0
In entity framework designer - i can see the field system_id ENTITY_KEY=true and StoredGeneratedPatern=Idenity
So I can not see what I have done to stop somthing from working with the entity framework, except updating the entity framework.
Any direction much appricated
thanks
When you added the new field, did you drop the table and recreate it?
If you did that then you deleted the trigger at the same time. So when you recreate the table, you also need to recreate the trigger.
Try inserting data just using an SQL statement and see if the id is generated.
This was an Entity Framework issue, one many have already had.
Not every time, but some times, when updating the model, StoredGenereatedPattern being droped in a section.
http://www.ladislavmrnka.com/2011/03/the-bug-in-storegeneratedpattern-fixed-in-vs-2010-sp1/
When looking at fixes, did not understand that BOTH SSDL and CSDL parts stored in same text.
So look in upper part that it has StoredGenereated Pattern.

Entity Framework 4 Change Audit

I have a web application using EF4. I am somewhat new to EF and now trying to implement change Audit.I tried to do this by trapping the SavingChanges event of the Context Class as below
partial void OnContextCreated()
{
this.SavingChanges += new EventHandler(TicketContainer_SavingChanges);
}
So the event handler accesses the changed records by the following
this.ObjectStateManager.GetObjectStateEntries(
EntityState.Added | EntityState.Modified);
This works fine and I am creating column level audit for selected tables. Every table/entity has an ID field which is an identifier with columnName="ID". So in my audit routine I simply accesses data from column with name "Id" to get the ID of audited record.
The problem I face is during insert . The new record has no ID yet as it is an identity column in the database and is always 0.
One solution I can think of is using GUID for all Ids.But is there a way to implement this using standard int32 Identity Ids?
thanks
When we insert data through EF the identity column is not generted while insertion. To get the Id of Identity columns we have to insert the data first then only we can get the Id of coulmn.
please go through the below which might be helpful to you.
http://www.codeproject.com/KB/database/ImplAudingTrailUsingEFP1.aspx
I don't now how many entities you have but in our own implementation of audit tracking we created a specific audit entity for each entity so we could link them together trough navigational properties and let the database set the identity keys.
If you use inheritance for your audit entities it's quit easy to query them.
Hope this helps :)
Identity columns are not generated while insertion. Once data is inserted then only you can get identity column data in EF. So, you can try some work around by getting Id after insertion and then populating audit table with that Id.

how to get next ID in a table in Rails

I have a table that has following columns
id store_id store_name
id and store_id are always going to be same. Since I am new to rails...I had created it by mistake. But I can't afford to go back and change stuff now since I am using store_id at a lot of places.
Issue 1: I am making admin screen for this table. When I try to insert a record, eventhough, id gets inserted (rails automatically gets the next one) NULL is being inserted in store_id.
Is there any ways I can have same number in store_id as in id while inserting record using the create method. Is there something that can be put in the model so that it is always execute before creating a record. and there I can have access to the next Id that should be inserted?
Issue 2: Since I am making admin screens for this table now..before I was just inserting data into this table by hand. I have inserted 5 records by hand so id and store_id 1..5 already exist. Now when I try to insert a record from admin screen it says duplicate key violates constraint. could it be trying to insert id of 1 since this is the first time I am inserting record to this table using rails?
Issue 1: Until you can remove the store_id column, override the getter in the model for compatibility.
def store_id
id
end
As for Issue 2: make sure your create code is not manually setting the id. The next record should be inserted with id 6, as long as your object has no id defined. Try it from script/console.
I think you should just change your code to only use the ID.
A competent editor will be able to tell you where you have used store id in your project, and your tests will help if you introduce a regression error.
Otherwise, you could use a database trigger. Or setup a callback on the model to set store_id from the newly created id value (look at the after_create callback). Again, both of these approaches are hacks to cover up a bug in your system with an easy fix.
You can set custom primary key for ActiveRecord models:
class Store < ActiveRecord::Base
set_primary_key :store_id
end
Then you can create a new migration that removes 'id' column and updates 'store_id' column to be a primary key.

Resources