I have thought:
HasMany(d => d.Categories)
.WithRequired()
.HasForeignKey(c => c.RelatedEntityId)
.WillCascadeOnDelete(true);
This piece of code mean that, d have required at least one category?
Related
I'm using the Entity Framework Core 6 fluent API to configure my database schema in a .NET Core project.
When declaring two-way relationships we can easily specify the foreign key like this:
modelBuilder.Entity<Foo>()
.HasMany(x => x.Bars)
.WithOne(x => x.Foo)
.HasForeignKey(x => x.FooId);
However, if we have a one-way only relationship like this:
modelBuilder.Entity<Foo>()
.HasOne(x => x.Bar);
I don't understand how to specify the foreign key, since the .HasOne method does not return an object that has the .HasForeignKey() method.
How do you specify the foreign key in these cases?
Try to do something like this:
modelBuilder.Entity<Foo>()
.HasOne(x => x.Bar)
.WithOne()
.HasForeignKey(e => e.Whatever);
Also this one maybe can help you too check
I am creating code first Entity framework and when I using fluent API to manage the mapping I have the following code
modelBuilder.Entity<Room>()
.HasOne(r => r.Creator)
.WithMany(u => u.Rooms)
.HasForeignKey(p => p.CreatorId)
.IsRequired()
.OnDelete(DeleteBehavior.SetNull);
so my question is there is any conflict in this chaining method as IsRequired() is used to make the foreign key not allowed to be null and the ".OnDelete(DeleteBehavior.SetNull);" set the foreign key to null when u (referred to the user) entity is deleted, so what would be the behavior of this chaining method when the user associated with this room is deleted, is the operation will be done and the foreign will be set to null or the operation will be restricted?
Yes, they will conflict. If you use DeleteBehavior.SetNull, your CreatorId will be set to nullable during the migration. However, if you set CreatorId to required, they will conflict and you will not be able to successfully migrate and successfully created the relationship.
If you need the CreatorId attribute to be required, then it is recommended that you useļ¼
modelBuilder.Entity<Room>()
.HasOne(r => r.Creator)
.WithMany(u => u.Rooms)
.HasForeignKey(p => p.CreatorId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
else,you can use:
modelBuilder.Entity<Room>()
.HasOne(r => r.Creator)
.WithMany(u => u.Rooms)
.HasForeignKey(p => p.CreatorId)
.OnDelete(DeleteBehavior.SetNull);
For more details,you can see this article: The Fluent API OnDelete Method.
Within my DBContext i have overriden the OnModelCreating method.
The same code works like a charm in .Net Core (by using ModelBuilder) but
on .NET Framework 4 it does not compile.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<User>(entity =>
{
entity.Property(e => e.CreatedBy)
.IsRequired()
.HasMaxLength(500);
}
}
The error i get is
No overload for method 'Entity' takes 1 arguments in Entity framework
Unfortunately i cannot figure out what is wrong with this one.
Any help would be greatly appreciated.
The same code works like a charm in .Net Core (by using ModelBuilder) but on .NET Framework 4, it does not compile.
In comparison of .net core with .net framework some method implementatios are diffrent. so it's the reason of your compile error. As a solution, just re-write your code as follow:
modelBuilder.Entity<User>()
.Property(e => e.CreatedBy)
.IsRequired()
.HasMaxLength(500);
base your need in the comment, we can manage more than one property in this method:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<User>(entity =>
{
entity.Property(e => e.CreatedBy)
.IsRequired()
.HasMaxLength(500);
entity.Property(e => e.ModifiedBy)
.IsRequired();
}
}
good luck.
EF Core was a complete re-write of Entity Framework; although there are many similarities, much of the entity configuration needs to be done differently.
The above would need to be:
modelBuilder.Entity<User>()
.Property(e => e.CreatedBy)
.IsRequired()
.HasMaxLength(500);
According to Enabling Cascade Delete on Microsoft's web site:
You can remove these cascade delete conventions by using:
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>()
The following code configures the relationship to be required and then disables cascade delete.
C#
modelBuilder.Entity<Course>()
.HasRequired(t => t.Department)
.WithMany(t => t.Courses)
.HasForeignKey(d => d.DepartmentID)
.WillCascadeOnDelete(false);
So, as far as I understood, Remove<OneToManyCascadeDeleteConvention>() removes cascade delete for all entities in this context, while WillCascadeOnDelete(false) only removes only the related entity (Course entity in the example above). Is that true?
I'm trying to ignore a property from source type. I have defined mapping like this:
var map = AutoMapper.Mapper.CreateMap<Article, IArticle>();
map.ForSourceMember(s => s.DateCreated, opt => opt.Ignore());
map.ForSourceMember(s => s.DateUpdated, opt => opt.Ignore());
When I call Map function,
AutoMapper.Mapper.Map(article, articlePoco);
destination's properties gets updated anyway. I'm using the latest stable version downloaded from NuGet.
Any ideas why this isn't working ?
I have found similar question to this one but there is no answer attached. [question]:AutoMapper's Ignore() not working?
If the property that you want to ignore only exists in the source object then you can you MemberList.Source in combination with the option method DoNotValidate(). See below:
CreateMap<IArticle, Article>(MemberList.Source)
map.ForSourceMember(src => src.DateCreated, opt=> opt.DoNotValidate());
map.ForSourceMember(src => src.DateUpdated, opt => opt.DoNotValidate());
This is perfect if you are using AssertConfigurationIsValid and want to ignore validation of certain source properties.
Change the mapping to use ForMember:
map.ForMember(s => s.DateCreated, opt => opt.Ignore());
map.ForMember(s => s.DateUpdated, opt => opt.Ignore());