Consider two classes.
public class File { [Key] public string Id { get; set; } public string Message_Id { get; set; } internal Message Message { get; set; } } public class Message { [Key] public string Id { get; set; } }
In EF6, for N : 1..0 relation there was this fluent API.
modelBuilder.Entity<File>() .HasOptional(e => e.Message ).WithMany().HasForeignKey(e => e.Message_Id);
What is equivalent in Entiity Framework Core 1?
Thank you
You can't put an EF6 context in an ASP.NET Core project because . NET Core projects don't support all of the functionality that EF6 commands such as Enable-Migrations require.
Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.
Entity Framework Core is what's known as an Object Relational Mapper (ORM). Created by Microsoft, the library allows developers to work abstractly with their database.
Entity Framework Classic ThenIncludeThe ThenInclude method moves the chaining level to the property included. It allows us to include related objects from the next level. ThenInclude is a syntactic sugar method to make it easier and clearer to include multiple related objects.
In EF Core you can use two ways for relating two tables:
Inside OnModelCreating
:
protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<File>() .HasOne(c => c.Message) .WithOne() .HasForeignKey(c => c.MessageId) }
Create new class FileConfiguration
and calling it inside OnModelCreating
:
public class FileConfiguration : IEntityTypeConfiguration<File> { public void Configure(EntityTypeBuilder<File> builder) { builder.ToTable("File"); // Id builder.HasKey(c => c.Id); builder.Property(c => c.Id) .ValueGeneratedOnAdd(); // Message builder.HasOne(c => c.Message) .WithOne(c => c.File) .HasForeignKey<Message>(c => c.MessageId) .OnDelete(DeleteBehavior.Restrict); } }
and inside OnModelCreating put below codes:
protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfiguration(new FileConfiguration()); }
You will not find an equivalent method in EF 7. By convention, a property whose CLR type can contain null
will be configured as optional. So what decide if the relationship is optional or not is if the FK property is nullable or not respectively.
In summary, due to your Message_Id
FK property is string
, it already accepts null
value, so if you use the following Fluent Api configuration:
modelBuilder.Entity<File>() .HasOne(s => s.Message) .WithMany() .HasForeignKey(e => e.Message_Id)
EF will configure your relationship as optional (or N : 0..1 as requested).
In case of your FK property is value type like int
, you should declare it as nullable (int?
).
Also I noticed now you have a navigation property with internal
access modifier. You should always declare your entity properties as public
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With