I'm using database first in EF6, let's say I have a table called Article with (ArticleID, title, text)
with the articleID as the primary key.
Then I add a table called RelatedArticle that has (ArticleID, RelatedArticleID), the primary key is both the articleid and the relatedarticleid with foreign keys back to the article for both.
The problem I'm having is when I pull it into the edmx file it only sets RelatedArticles as a navigation property off of an article so when I go to delete, it only pulls the relatedarticles where the articleid is the article's id I'm trying to delete. Well as you can imagine, I need to be able to say
delete any records where the articleid OR the related articleid is the current article's id that I'm trying to delete.
Or I get big ole FK constraint errors.
Typically when this happens I at least have a "table" off of the context that I can just query manually and kill what I need to. (for example:
context.RelatedArticles.Where(ra => ra.ArticleID == articleID OR
ra.RelatedArticleID == articleID)
But like I said it only pulls in the related articles as a navigational property off of the Article itself.
Ideas?
Related
I have the following Table Structure
Facility
PK Facility ID
AccountID
Accounts
PK NameID
PK AccountID
I can't touch the DB so my changes need to be in Entity Framework. Essentially the AccountIDs are linked so I want to create an association between them. So when I create an association I map the AccountIDs together, however I can't map FacilityID to anything and NameID to anything so when I save Visual Studio complains that the mapping is not set correctly.
My main question is how do I ignore the mappings for FacilityID and NameID? I've tried added [NotMapped] to both FacilityID and NameID but that does not work. I've also tried creating a scalar property for Facility and Accounts and used the Referntial Constraint to map them however when I try to map the columns under Table Mapping, the columns I added do not show up which causes VS to complain as well.
Here is my table, I removed most of the fields because they are unnecessary
Assuming Account.AccountID is unique (ie no two rows in Account actually have the same AccountID), just declare that as the only Key Property on the Account entity.
The Key of an entity does not have to be declared as the PK in the database. But you can only have one Key per entity (the Key can, of course, have multiple columns, and EF Core does support alternate keys). The entity Key should be unique, and should have a unique index in the database on the corresponding columns, but that's not enforced by EF.
I have table(and also entity in Entity Data Model) Person with following fields:
Name Type
SocialID String
FirstName String
LastName String
which SocialID is Primary Key. I want to update value of SocialID for each record. However when i try to update this field in Entity Framework I get following error:
The property 'SocialID' is part of the object's key information and cannot
be modified.
The code that i get above error is:
foreach (var p in Entity.Persons)
{
p.SocialID= p.SocialID + "00";
Entity.SaveChanges();
}
How I can do this??
As mentioned by the others, you can't do it in code. You will have to make your update in SQL. Either in a migration or directly in SQL Server Management Studio (or the equivalent if you're using a different database).
UPDATE Person -- Or 'Persons' if that's what your table is called
SET SocialID = SocialID + '00'
It will require a lot more work than this if you have other tables use this column as a foreign key (you'll have to drop the constraints first -- on all tables that reference your primary key -- then fix the data and recreate the constraints). Or as Moe said in the comments, you can set your foreign keys to cascade on update.
As per my knowledge primary key once generated cant be updated programatically,that defies the purpose of primary key.
It'll be better if you insert all your data again with new primary keys and delete old data.
Why would you want to change the primary key? Entity framework will be using that field to identify the object, you can't change its value while it is the primary key.
Based on the first answer I suggest you change the table Person to have its own primary key, let's say PersonID and mantain the SocialID as a foreign key to the Social table. If you need a person to have several Social records you may need to create other table to correspond the PersonId to several SocialId, removing the SocialId from the person table.
I followed this article on making a table-per-type inheritance model for my entities, but I ran into some issues. Below I'm posting steps to reproduce the problem I'm having in a minimal environment to rule out other factors.
First, I created a new MS SQL Server 2008 R2 database with the following two tables:
Users
Id : bigint (PK and set it as the identity column)
Name : nvarchar(25) NOT NULL (whatever, some random property
Customers
Id : bigint (PK, identity column, and FK on Users.Id)
Title : nvarchar(25) NOT NULL (also whatever, some random property)
Next, I generated the .edmx entity model from the database, and followed the link at the top verbatim. That is to say, I deleted the association between User and Customer, set User as the base class of Customer, deleted the Id property from the Customer entity, and made sure that the Customer.Id column was mapped to the inherited User.Id property. I then ran the following small program:
using (var db = new EF_Test.testEntities())
{
var cust = db.Users.CreateObject<Customer>();
db.Users.AddObject(cust);
db.SaveChanges();
}
I get the following error when I make that last call:
"A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns."
With the following inner exception:
"An item with the same key has already been added."
Any ideas on what I could be missing?
A quick google on the error message turned up the following solution, maybe it helps you:
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/4bfee3fd-4124-4c1d-811d-1a5419f495d4
I think that I figured it out. The
table for the Party sub type had its
key column set to autogenerate a key
value and since it's derived, the EF
wanted to set that value explicitly.
So have you tried removing the "identity" setting from the customer table? So it doesn't autogenerate the primary key?
Hope this helps.
I finally found the source of my troubles. For those still interested, in the Customers table, the Id column should not have been set to the identity column of the table (PK and the FK dependency are fine though).
Why you don't want to make a foreign key (UserId) as a separate column? Maybe it can help you.
Also try to use model first approach and generate db after model creation as it is described in the following article.
I have a Tags table and an Objects table.
A record of their relationships is kept via the ObjectTags table which has two columns. The columns store ObjectId and TagId (from the Tags and Objects tables), and both make up a composite key (can't have TagId and ObjectId twice).
In Entity Framework this table is not mapped as an object but rather enable "navigation" between the main tables. This is very cool, but how to i add to this table? What is the best way?
I add an Object and now i have it's ObjectId. I also add new Tags (reuse the already existing ones) and get their TagId. Now i should add the ObjectId and TagId to this relationship table... but how?
I had to use something like this:
db.Items.Where(id => id.Id == newItem.Id).First().Tags.Add(newTag);
This means. In the "db" in the "Items" table where "the Item Id is the one i'm looking for", select it, then go via the navigation property to the "Tags" table and add a "new Tag".
Doing this updates the relationship's ItemTags table with the new ids.
:)
I have a database with multiple tables, and some basic relationships. Here is an example of the problem I am having:
My Database:
**Org**
ID
Name
etc
**Detail1**
ID
D1name
**Org_Detail1**
Org_ID
Detail1_ID
**Detail2**
ID
D2Name
**Org_Detail2**
Org_ID
Detial1_ID
BooleanField
My problem is, the Org_detail1 table is not showing up in the entity model, but the Org_Details2 table does.
I thought it may have been because the Org_Detail1 table only contains two ID fields that are both primary keys, while the Org_Details2 table contains 2 primary key ID fields as well as a boolean field.
If I add a dummy field to Org_detail1 and update it, it still won't show up and wont allow me to add a new entity relating to the Org_Detail1 table. The table won't even show up in the list, but it is listed under the tables.
Is there any solution to get this table to appear in my model?
Seems like I may just need to completely delete the model and recreate it. Adding dummy fields is the only solution I've found.
Not sure this is an MVC problem.
Does a Detail1 collection turn up in your Org entity and an Org collection show up in your Detail1 entity. This is the normal behaviour for Entity framework for a many to many intersecting table with no other tangible data.
If not then maybe the foreign key constraints are not defined.