How to save ICollection <int> with EF CTP5

I have the following properties for my class: Team

[Key] public virtual long Id { get; set; } public Guid ClubIdentifier { get; set; } public GenderEnum Gender { get; set; } public TeamAgeCategoryEnum TeamAgeCategory { get; set; } public ICollection<int> BirthYears { get; set; } 

How to save content in the BirthYears property in my database, I allow EF to create my model-based database, but the BirthYears property is not in my database. I would expect the new table to contain an int value and a Team ID value.

What I missed, I think I need to do something with the OnModelCreating method in my repository class.

+4
source share
1 answer

If you look at the EntityTypeConfiguration<TEntityType> class, you will see the following signature for defining a one-to-many relationship (this is your relationship between Team and BirthYears ):

 HasMany<TTargetEntity>(Expression<Func<TEntityType, ICollection<TTargetEntity>>> navigationPropertyExpression) where TTargetEntity : class; 

As you can see, there is a where TTargetEntity : class constraint that requires BirthYears be a collection of class objects. int not a class, so mapping will not be possible.

The only workaround I see is to define a small class ...

 public class BirthYear { public int Id { get; set; } public int Value { get; set; } } 

... and then use this in your collection in the Team class:

 public ICollection<BirthYear> BirthYears { get; set; } 

Mapping agreements should automatically create a one-to-many relationship, so you don’t need the Fluent API to set up associations.

Edit

Correction according to Ladislav the correct critic in the comments:

The BirthYear class requires the optional Key property. I added the Id property.

I also assume that BirthYears will be a Team dependent property. Mapping agreements will create an optional relationship from BirthYear to Team . I think it would be more appropriate for the model to make this relationship necessary using the Fluent API:

 modelBuilder.Entity<Team>() .HasMany(t => t.BirthYears) .WithRequired(); 

This will automatically enable cascading deletion - the associated BirthYears will be deleted from the database when the command is deleted.

Edit 2

(Again, based on Ladislav's comment). If you do not want to copy the years in the BirthYears table, you can also configure many-to-many relationships:

 modelBuilder.Entity<Team>() .HasMany(t => t.BirthYears) .WithMany(); 

This will add a join table ( TeamBirthYears ) between Team and BirthYear to the database. In terms of storage space or performance, you are likely to gain nothing (since the BirthYear class BirthYear very small and the entry in the BirthYear table is the same size as the entry in the join table). But this may be the best approach if you want to extend the BirthYear class BirthYear additional properties sooner or later. Otherwise, I personally would have left him simple in a One-to-Many relationship. But the choice is yours.

+5
source

Source: https://habr.com/ru/post/1346915/


All Articles