Entity Framework 4.1 Code First, One-to-One with one table joining to a single key field of of composite key - entity-framework-4.1

I'm just beginning with EF4.1 Code First, and I pretty like it.
Here's the story :
A Codif class is composed of a key from a Domaine class, an Entite class, and Reference class, and a fourth field which is some text.
Reference and Codif have an one-to-one relationship.
Thing is, when it creates the Database, it creates some ugly fields in my Reference entity, creating duplicate fields of the Codif Entity.
Good point : When I manipulate my Reference object however, I have the expected behaviour of accessing the Codif property, and the duplicate fields are invisible.
Here's the code :
public class Reference
{
public int ReferenceId { get; set; }
public string Libelle { get; set; }
public virtual Codif Codif { get; set; }
}
public class Domaine
{
public int DomaineId { get; set; }
public string Libelle { get; set; }
}
public class Codif
{
[Key, Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int DomaineId { get; set; }
[InverseProperty("DomaineId")]
public virtual Domaine Domaine { get; set; }
[Key, Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EntiteId { get; set; }
[InverseProperty("EntiteId")]
public virtual Entite Entite { get; set; }
[Key, Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ReferenceId { get; set; }
[InverseProperty("ReferenceId")]
public virtual Reference Reference { get; set; }
[Key, Column(Order = 3)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string Codification { get; set; }
}
public class Entite
{
public int EntiteId { get; set; }
public string Nom { get; set; }
public string Email { get; set; }
}
And here's the result in the tables (images) :
Codif
Reference
In the Reference class, how can I specify that ReferenceId is the foreign key to be used against a SINGLE field of Codif ?
How to get rid of those duplicate fields in Reference ?
How to remove the Reference_ReferenceId in Codif table while preserving the navigation property ?
Thank you for your support.
Marc
Edit : I'm working with an SQL Compact Edition 4 database

Replace all your [InverseProperty("xxx")] attributes by [ForeignKey("xxx")]. I think that this is what you actually want. [InverseProperty] refers to the navigation property on the other side of the relationship which can never be a scalar property.
Edit
You could in addition to the FK attribute set the [InverseProperty] attribute on the Reference property in your Codif class:
public class Codif
{
//...
[Key, Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ReferenceId { get; set; }
[ForeignKey("ReferenceId")]
[InverseProperty("Codif")] // <-- refers to Codif property in Reference class
public virtual Reference Reference { get; set; }
//...
}
But it think it's not really necessary because EF should detect the correct relationship between Reference and Codif by convention. (I'm not sure though for one-to-one relationships.)
Edit
Problems!
First: As far as I can see you must specify the one-to-one relationship in Fluent API because EF cannot determine otherwise what's the principal and what's the dependent:
modelBuilder.Entity<Reference>()
.HasOptional(c => c.Codif)
.WithRequired(c => c.Reference);
Second: EF will still complain because of the composed key or because ReferenceId is not the key alone. If ReferenceId were the only key in Codif it would work.
Edit
I'm trying to understand what you want to achieve. Apparently your composed key in Codif is supposed to ensure that any combination of the four field values can exist only once. But this conflicts with the one-to-one relationship imo, for example this would be valid table entries:
Table Codif:
DomaineId EntiteId ReferenceId Codification
----------------------------------------------------
1 1 1 "A"
2 1 1 "A"
1 2 1 "A"
1 1 2 "A"
1 1 1 "B"
etc...
But as you can see: You can have multiple rows with the same ReferenceId which means that you cannot have a one-to-one relationship to Reference. Here you have 4 Codif entities which refer to the same Reference entity with Id = 1.
Now, I guess, the fact that you want to have a one-to-one relationship means that there is an additional constraint so that ReferenceId in Codif table can occur only once. In other words: The rows 2, 3 and 5 in the example above are invalid from business viewpoint (although valid from DB viewpoint).
If this is the case I would actually make ReferenceId the single key in Codif and make sure from business logic that the other combinations of values in the DB are unique (Query if exists before you insert a new Codif). On database side you could create a unique index over the other three fields to ensure that the combinations are always unique in the database. EF cannot check this internally though since unique constraints are not yet supported.

Related

Enity Framework Core: Handling large number of enum-like fields

I am currently facing the following problem:
I have a model class LargeDataClass with many fields (200+).
Many of these fields (~50-80) are enum-like (i.e. they can be filled out with certain sets of options in the UI).
Now my approach was to model these as enum classes, like
[Table("tbl_enum_one")]
class EnumOne {
public int ID { get; set; }
public string Name { get; set; }
}
[Table("tbl_large_dataclass")]
class LargeDataClass {
public EnumOne EnumOne { get; set; }
public int EnumOneId { get; set; }
//...
}
This has the major advantage of being easily extendable (to add a dropdown option in the UI, just add a row to the table).
Now I am facing some concerns/problems:
When I fetch my model class LargeDataClass from the DB with all its enum fields included, there will be a lot of joins (as I stated above, there are like 50 to 80 of these fields). I am worried that will have a big impact on query performance. Plus create/update/delete might be quite slow due to the large number of indexes to be updated.
MySQL won't even let me create a table tbl_large_dataclass with that many FKs (too many indexes on a single table).
So now I am considering two (in my view really unfortunate) options:
Using regular enums, so no enum classes with their own tables, storing them as simple int/string fields in the DB. This would cause no performance concerns at all, but unfortunately, the 'live' extendability is quite important, so this option would only be the last resort.
Using the Enum classes, but having just the ID of the enum in the LargeDataClass, so kind of keeping the fact that this is a foreign key secret from the DB. If I wanted to display a LargeDataClass object somewhere, I would have to separately fetch the enum classes. Plus I would have to make extra sure everywhere that I only use Ids that are really present in the enum table.
I am really unsure what would be the best approach here.
Database is not an object store and you have to design it accordingly. I have changed you schema and only two tables are needed for storing dropdown values.
[Table("tbl_enum_type")]
public class EnumType {
public int ID { get; set; } // PK
public string Name { get; set; }
}
// PK (EnumTypeId, Id) - reusing the same index for dropdown generation
[Table("tbl_enum_value")]
public class EnumValue {
public int ID { get; set; }
public string Name { get; set; }
public int Order { get; set; } // for dropdown ordering
public int EnumTypeId { get; set; }
public EnumType EnumType { get; set; }
}
// store only ID's, no FK
[Table("tbl_large_dataclass")]
public class LargeDataClass {
public int EnumOneId { get; set; } // EnumTypeId 1
public int EnumSecondId { get; set; } // EnumTypeId 2
//...
}
For generating dropdowns, you have to cache EnumType and EnumValue tables in memory in useful structure.
Override method SaveChanges/SaveChangesAsync and check saved Id's according to cached data.
It will not help if your database is changed via SQL, but here we have trade-off between performance and consistency. Probably good trigger may help here.
UPDATE:
Consider to restructure LargeDataClass to two tables
[Table("tbl_option_bag")]
public class OptionBag {
public int Id { get; set; }
public ICollection<Option> Options { get; set; }
}
[Table("tbl_options")]
public class Option {
public int Id { get; set; }
public int OptionBagId {get; set; }
public int EnumTypeId { get; set; }
public int EnumId { get; set; }
//...
}
Here you can use FK and DTO can be generated on selecting Options navigation property.

Entity Framework 4.1 Code first mapping to tables that have their primary key as the foreign key column

I have an existing database that I'm using Entity Framework Code First to map. The naming convention for columns is odd, so I decided I'd map entity properties manually, and up until now this has been fine.
The schema for the database is fairly strange to me and is definitely not how I would've done it. Unfortunately, I'm stuck with it for the time being.
Basically there is a single primary key (AccountNumber) shared by a number of tables creating a bunch of one-to-one relationships. However, the primary key is also the foreign key column. Here is what my entities look like (with a whole bunch of properties removed for simplicity). I've only included two entities to make it easy.:
public class Customer
{
public int AccountNumber { get; set; }
public String PhoneNumber { get; set; }
...
public virtual Address Address { get; set; }
}
public class Address
{
public int AccountNumber { get; set; }
public String Name { get; set; }
public String Address1 { get; set; }
public String City { get; set; }
...
public virtual Customer Customer { get; set; }
}
The two entities share the same primary key. I've created configuration classes to do the mapping like this:
public class CustomerConfiguration : EntityTypeConfiguration<Customer>
{
public CustomerConfiguration()
: base()
{
HasKey(p => p.AccountNumber);
Property(p => p.AccountNumber).
HasColumnName("cm_l_acct").
IsRequired();
Property(p => p.PhoneNumber).
HasColumnName("cm_s_phonenumber");
HasRequired(x => x.Address).
WithRequiredPrincipal(x => x.Customer).
Map(x => x.MapKey("am_l_acct"));
}
}
public class AddressConfiguration : EntityTypeConfiguration<Address>
{
public AddressConfiguration()
: base()
{
HasKey(p => p.AccountNumber);
Property(p => p.AccountNumber).
HasColumnName("am_l_acct").
IsRequired();
...
}
}
The foreign key mapping is only done on one side. This appears like it would work if not for the fact that the foreign key column is also the primary key of the table. When I try to run a query, I get the error:
(256,6): error 0019: Each property name in a type must be unique. Property name 'am_l_acct' was already defined.
Unfortunately, I can't pull the mapping for the AccountNumber property off of the Address entity because it is the primary key.
Is there a way I can accomplish this mapping, or is it impossible?
Removet this Map(x => x.MapKey("am_l_acct")) from your Customer mapping. This mapping is only used if you map want to define FK column in database and you don't have FK property in the class but you have it - it is primary key in the Address entity. If you try to map FK that way EF thinks that you are trying to create to columns with the same name.

Entity Framework Code First: FOREIGN KEY constraint may cause cycles or multiple cascade paths

Entity Framework Code First can generate the DB for the following POCOs.
public class Item {
public int Id { get; set; }
public string Name { get; set; }
}
public class ItemPair {
public int Id { get; set; }
public virtual Item FirstItem { get; set; }
public virtual Item SecondItem { get; set; }
}
I would like to establish the relationship with First and Second item via ID fields rather than the an entire "Item" class. So:
public class ItemPair {
public int Id { get; set; }
public virtual Item FirstItem { get; set; }
public int FirstItem_Id { get; set; }
public virtual Item SecondItem { get; set; }
public int SecondItem_Id { get; set; }
}
also works. Edit: This didn't actually work. Just generates additional FirstItem_Id1 and SecontItem_Id2 columns.
But just changing the foreign key properties to FirstItemId, SecondItemId, (without the underscore) like so:
public class ItemPair {
public int Id { get; set; }
public virtual Item FirstItem { get; set; }
public int FirstItemId { get; set; }
public virtual Item SecondItem { get; set; }
public int SecondItemId { get; set; }
}
results in the following exception.
{"Introducing FOREIGN KEY constraint 'ItemPair_SecondItem' on table 'ItemPair' may cause
cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION,
or modify other FOREIGN KEY constraints.\r\nCould not create constraint.
See previous errors."}
Why? And what can I do to avoid this exception.
I decided to just remove the cascade delete convention.
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}
The reasons are:
I prefer to mark records deleted or deactivated for audit purposes.
At most I delete just the junction / mapping tables.
With an ORM It is relatively trivial to loop through and delete child records in the rare case I need to.
Thank you Ladislav Mrnka pointing me in the right direction.
My expectation is that in the first case your Id properties are not used in the database as FKs and EF will create another two columns (you can validate this by forcing pairing of navigation property with FK property using ForeignKeyAttribute). In the second case EF will correctly recognize your properties but it will also use cascade delete convention which will cause error in SQL server. You have two properties from the table pointing to the same parent. Actually in the database you can createItemPair from the same Item (both FKs set to the same Id). If both relations have cascade delete enabled it will result in multiple cascade paths => not allowed in SQL server.
The solution here is fluent mapping to manually define how the relations are mapped. Here is the example.

EF Code First unidirectional One-To-Many with Data Annotations

Say I have the following POCO classes:
public class Parent
{
public int ID { get; set; }
}
public class Child
{
public int ID { get; set; }
public int MyParentID { get; set; }
[ForeignKey("MyParentID")]
public Parent MyParent { get; set; }
}
The Child.MyParent property maps to the Parent table with a one-to-many relationship, but I don't want the Parent class to be aware of the association (unidirectional). I can do this within the DbContext.OnModelCreating (or any of its equivalents) with the following:
modelBuilder.Entity<Child>()
.HasRequired(c => c.MyParent)
.WithMany()
.HasForeignKey(c => c.MyParentID);
But, I can't seem to find the same with data annotations. Is there such a thing? The ForeignKey annotation I am using seems to require bidirectionality, because it gives me the "Unable to determine the principal end of an association" exception until I add an ICollection<Child> property on the Parent class
UPDATE
This code should actually work as-is. The issue I was trying to isolate in my code didn't actually involve this setup. I've posted a new question regarding my problem here.
public class Child
{
public int ID { get; set; }
[ForeignKey("Parent")]
public int ParentID { get; set; }
public Parent Parent { get; set; }
}
should work. Actually you don't need to do anything - neither in Fluent API nor with annotations - because EF conventions will exactly create the relationship automatically you have defined in Fluent API. The foreign key will be detected because it has the name pattern [Navigation property]Id, the relationship will be "required" because the FK is non-nullable and it will be one-to-many because you have a single reference (Parent) on one side and "many" on the other side is default if there is no corresponding navigation property.

One To Many Relationship - Cascading Delete

I'm using EF 4.1 where I'm trying to map my POCO to my existing database. This is working fine until I try to delete an item that the other item has a dependency to. I want to enable cascading deletes, so that when my first item is deleted all dependencies would also be deleted (I believe this is called cascading delete).
I tried to enable this in the OnModelCreating:
modelBuilder.Entity<Component>()
.HasMany(c => c.Specifications)
.WithRequired(s => s.Component)
.Map(m => m.MapKey("ComponentId"))
.WillCascadeOnDelete(true);
However, I still get the The DELETE statement conflicted with the REFERENCE constraint exception.
The database is quite simple:
Component:
ComponentId (PK)
Description
Specification:
SpecificationID (PK)
Description
ComponentID (FK)
I've created the two following classes to match this setup:
public class Specification
{
[Key]
[Required]
public int Id { get; set; }
[MaxLength(50)]
[Required]
public string Description { get; set; }
public virtual Component Component { get; set; }
}
and
public class Component
{
[Key]
[Required]
public int Id { get; set; }
[MaxLength(50)]
[Required]
public string Description { get; set; }
public virtual ICollection<Specification> Specifications { get; set; }
}
Cascading delete in your model requires cascading delete in your DB. If you let the EF recreate the DB for you, it will set this up automatically. If you cannot let the EF do this, then you must either:
Add cascading delete to the FK manually, or
Remove the cascade from the model.