Error in updating edmx file - c#

I am getting an error when I am updating the edmx file.
Error 3002: Problem in Mapping Fragment starting at line 3828:
Potential runtime violation of table Archive's keys (Archive.UserID):
Columns (Archive.UserID) are mapped to EntitySet Archive's properties
(Archive.UserID) on the conceptual side but they do not form the
EntitySet's key properties (Archive.ListID, Archive.UserID).
Any idea how to resolve this?

Check the mapping between the two tables. It sounds like the properties are mapped to the wrong field.
Take a look at: http://cticoder.wordpress.com/2008/10/14/entity-framework-error-3002-error-3003/

Related

Modelling polymorphic associations database-first vs code-first

We have a database in which one table contains records that can be child to several other tables. It has a "soft" foreign key consisting of the owner's Id and a table name. This (anti) pattern is know as "polymorphic associations". We know it's not the best database design ever and we will change it in due time, but not in the near future. Let me show a simplified example:
Both Event, Person, and Product have records in Comment. As you see, there are no hard FK constraints.
In Entity Framework it is possible to support this model by sublassing Comment into EventComment etc. and let Event have an EventComments collection, etc.:
The subclasses and the associations are added manually after generating the basic model from the database. OwnerCode is the discriminator in this TPH model. Please note that Event, Person, and Product are completely different entities. It does not make sense to have a common base class for them.
This is database-first. Our real-life model works like this, no problem.
OK. Now we want to move to code-first. So I started out reverse-engineering the database into a code first model (EF Power Tools) and went on creating the subclasses and mapping the associations and inheritance. Tried to connect to the model in Linqpad. That's when the trouble started.
When trying to execute a query with this model it throws an InvalidOperationExeception
The foreign key component 'OwnerId' is not a declared property on type 'EventComment'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.
This happens when I have bidirectional associations and OwnerId is mapped as a property in Comment. The mapping in my EventMap class (EntityTypeConfiguration<Event>) looks like this:
this.HasMany(x => x.Comments).WithRequired(c => c.Event)
.HasForeignKey(c => c.OwnerId);
So I tried to map the association without OwnerId in the model:
this.HasMany(x => x.Comments).WithRequired().Map(m => m.MapKey("OwnerId"));
This throws a MetaDataException
Schema specified is not valid. Errors:
(10,6) : error 0019: Each property name in a type must be unique. Property name 'OwnerId' was already defined.
(11,6) : error 0019: Each property name in a type must be unique. Property name 'OwnerId' was already defined.
If I remove two of the three entity-comment associations it is OK, but of course that's not a cure.
Some further details:
It is possible to create a working DbContext model ("code second") from the edmx by adding a DbContext generator item. (this would be a work-around for the time being).
When I export the working code-first model (with one association) to edmx (EdmxWriter) the association appears to be in the storage model, whereas in the original edmx they are part of the conceptual model.
So, how can I create this model code-first? I think the key is how to instruct code-first to map the associations in the conceptual model, not the storage model.
I personally stick with Database first when using EF on any schema that is this level of complexity. I have had issues with complex schemas in regards to code first. Maybe the newer versions are a little better, but worrying how to try and code complex relationships seems less straight forward then allowing the engine to generate it for you. Also when a relationship gets this complex I tend to avoid trying to generate it with EF and try and use stored procedures for easier troubleshooting of performance bottlenecks that can arise.

edmx validation throws an error about collide primary key

I have 3 entities: BaseUser, AuthenticationUser and User. AuthenticationUser inherit from BaseUser and User inherit from AuthenticationUser. User entity has scalar property of type Binary (photo) so I would like to split the User entity into User and UserPhoto.
I did exactly what this nice article sais: http://www.deveducate.com/blog/post/2010/12/14/Entity-Framework-Modeling-Table-Splitting.aspx
Here is an image of my edmx:
When I validate the edmx I keep getting this error:
Error 3033: Problem in mapping fragments starting at line 13058:EntitySets 'UserPhotoes' and 'BaseUsers' are both mapped to table 'T_USER'. Their primary keys may collide.
What is the meanning of this error?
Any idea how to fix this?

Entity Framework database-first with SQL Server

I'll try to explain my problem although to be honest I can't even understand it. After many changes in a couple of tables in my DB now I try to create a Foreign key and I'm getting this error when updating the EDMX.
gHOP.msl(410,10) : error 3007: Problem in Mapping Fragments starting at lines 410,
1511: Non-Primary-Key column(s) [UserGUID] are being mapped in both fragments to different conceptual side properties - data
inconsistency is possible because the corresponding conceptual side
properties can be independently modified.
gHOP.msl(1511,6) : error 3012: Problem in Mapping Fragments starting at lines 410, 1511: Data loss is possible in Itinerary.UserGUID.
An Entity with Key (PK) will not round-trip when:
(PK does NOT play Role 'Itinerary' in AssociationSet 'FK_Itinerary_Users' AND PK is in 'Itinerary' EntitySet)
gHOP.msl(410,10) : error 3012: Problem in Mapping Fragments starting at lines 410, 1511: Data loss is possible in Itinerary.UserGUID.
An Entity with Key (PK) will not round-trip when:
(PK is in 'Itinerary' EntitySet AND PK does NOT play Role 'Itinerary' in AssociationSet 'FK_Itinerary_Users' AND Entity.UserGUID
is not NULL)
Honestly, it's been a nightmare because I can't understand what's going on. Although I've given up and I won't create the FK if someone could at least give me a hint I would really appreciate it.
Thanks
Not sure how much access/control you have over the model but I've encountered similar issues before when updating entities in the db and then attempting to update the model. I usually just delete the entities from the model which you edited in the db! Rebuild without them. Then re-add them after the build. I find that Visual Studio is not always successful updating the model when structural/relational changes have been made in the db.

Need help using entities for ASP.NET MVC 2 framework

I'm making a site in ASP.NET MVC 2 using C#. I designed a database with a bunch of tables that have many to many relationships, similar to the following:
GrandParent - many to many - Parent
and
Parent - many to many - Child
I used the Entity Framework to make all of the entities class and am now working on a function in a repository class which adds a GrandParent.
It tries to create Child, add it to a Parent, then add the parent to a GrandParent and then add the GrandParent to the database by using the command
entities.GrandParents.AddObject(newGrandParent);
entities.SaveChanges();
It crashes on the SaveChanges() line with the error:
Unable to update the EntitySet
'JunctionPartentsChilds' because it
has a DefiningQuery and no
element exists in the
ModificationFunctionMapping element to
support the current operation.
EDIT:
I can fix that error by deleting all of the DefiningQuery elements in the auto generated code and now I'm getting an error on the same line that should be more descriptive but I'm still at a loss.
The error is: Invalid object name 'JunctionPartentsChilds' and it throws a UpdatingException.
Any ideas what's going wrong? Do you have to add to the database in a special order because of the many to many relationships?
Thanks!!
If you copy/pasted this error:
Invalid object name 'JunctionPartentsChilds'
Maybe the problem is the misspelling of "Parents" in the junction table name
Your description looks like to one to many relationsships. But anyway, please post your code fragment here, to see if there's an error in your statements.
Bye Thomas
P.S. Ther's a wonderful way to format your code like this
This is a line of code
if you klick the button with the 101
make sure that GrandParents table as a primarykey(identity) and rebuild the model

Problems with automatic property creation when manually mapping relationships between tables with no foreign keys in L2S

Pardon the massive headline.
I'm in the situation of having to build an application on top of a database, that I cannot make any changes to. The database does not have any primary- or foreignkeys set.
I'm using linq-2-sql, and I'm interested in having some properties exposed on the entities generated from my dbml. For instance, in the hypothetical example of a one-to-many relationship between table education and student - where each student record has a reference to an education id, I'd like to be able to go:
var student = GetAStudentFromContextOrWhatever();
var studentsEducation = student.Education;
It is my experience, that this kind of property is automatically generated when I drag'n'drop tables with foreignkey relationships from the server explorer.
However as previously mentioned, in this case I do not have these foreign key relationships - rather I am adding the relationships manually in the dbml file, specifying parent and child class.
When I add these relationships, I expect the involved entities in the designer.cs of my context to get populated with properties of a kind like those described above.
This, however, does not happen.
What must I do for my dbml to create these properties for me - based on these manually mapped associations between entities/tables that, on a database level, do not have foreign key associations?
Cheers!
L2S is just that Linq-to-SQL. If it isn't in SQL it won't be generated. The expression trees behind L2S just can't understand what you are doing. The place for your association is in a partial class file which you will have create manually. Also it probably won't update or insert through the association.
I know this is a very old question, but I just ran into the same problem. In order for the relationship in the DBML designer to automatically create the association properties for you, you need to have primary keys on your objects. If you click the column name in the designer, you'll see that your PK field has PrimaryKey = false. Switch that to True and build; all should be well.
Patrick

Categories

Resources