EntityFramework - Port WithRequired to EF Core - entity-framework-6

I'm porting an application from MVC5/EF6 to MVC6/EF7, but having an issue with this particular line:
modelBuilder.Entity<Client>().HasMany(c => c.Payments).WithRequired(e => e.Client).WillCascadeOnDelete(false);
Apparently the WillCascadeOnDelete is transformed to the OnDelete with restrict as parameter, but I can't find any documentation on the "WithRequired" part which has disappeared too in EF7. Has 'WithOne' the same impact or am I completely wrong here :
modelBuilder.Entity<Client>().HasMany(c => c.Payments).WithOne(e => e.Client).OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict);

You are correct in both cases. Here is a detailed post... http://www.mikee.se/posts/migrating_from_ef6_to_ef_core
Typically these mappings would change from this in EF6.
x.Entity<Company>()
.HasMany(c => c.StatementOfEarnings)
.WithRequired(e => e.Company)
.WillCascadeOnDelete(false);
To this in EF Core
x.Entity<Company>()
.HasMany(c => c.StatementOfEarnings)
.WithOne(e => e.Company)
.OnDelete(DeleteBehavior.Restrict);

Related

Breezejs clear multple keys

We have the following mapping in EF6:
HasRequired(x => x.modulemain).WithMany().HasForeignKey(t => t.moduleid);
HasOptional(t => t.modulerslt).WithMany().HasForeignKey(t => new { t.moduleid, t.trmendrs });
HasOptional(x => x.modulegoal).WithMany().HasForeignKey(t => new { t.moduleid, t.trmgcode });
HasOptional(x => x.modulefase).WithMany().HasForeignKey(t => new { t.moduleid, t.trmfcode });
Breezejs understands this mapping with the combination of the two keys. BUT there is a problem: Whenever the user 'clears (sets its value to NULL)' the modulegoal/modulefase/modulerslt sets the moduleid value to an empty string and the 'second key' as well. The problem is that the first keys is being used in multiple references. So clearing one of the references, mismatches the other (important) references. Is there any way to tell breeze to NOT clear the first key?

Trim String In Automapper

Mapper.CreateMap<DataViewModel, DataSource>()
My Source Here Contains String Values Coming from user interface. I want to trim all the string before i map it to my destination object. Couldn't find a solution for this. Anyone Knows how this is to be done
This can be done using the ForMember method, like so:
Mapper.CreateMap<DataViewModel, DataSource>()
.ForMember(x => x.YourString, opt => opt.MapFrom(y => y.YourString.Trim()));
If you want to trim more than one property you can chain the .ForMember() method like this:
Mapper.CreateMap<DataViewModel, DataSource>()
.ForMember(x => x.YourString, opt => opt.MapFrom(y => y.YourString.Trim()))
.ForMember(x => x.YourString1, opt => opt.MapFrom(y => y.YourString1.Trim()))
.ForMember(x => x.YourString2, opt => opt.MapFrom(y => y.YourString2.Trim()));
Whilst this would get the job done, I would suggest performing input sanitisation elsewhere in your application as it doesn't belong in the mapping.
You can also use AddTransform
CreateMap<DataViewModel, DataSource>()
.AddTransform<string>(s => string.IsNullOrWhiteSpace(s) ? "" : s.Trim());

ASP.NET Codefirst relationships

I made this relationships in codefirst but it shows exactly this error that shows in this image. I upload my models for you and you can download it from here[4kb]. what is this error? why it shows this? I want to have all my categories in posts table. so what is way? who can help?
someone tell me what is wrong?
By convention in Entity Framework, deleting an entity will delete anything that is linked to that antity. In your case that means deleting cat1 will delete every cat2, cat3 and post that is referencing cat 1.
Your problem occurs because you specify WillCascadeOnDelete(true) on the posts map that you also want a post to automatically trigger a deletion of the cat1, cat2 and cat3 that the post references which can cause a partial or complete wipe of the database (posts delete categories that delete posts that delete categories and so on).
To fix it you need to remove your WillCascadeOnDelete(true)-mappings from postMap.
this.HasRequired(t => t.cat1)
.WithMany(t => t.posts)
.HasForeignKey(d => d.excat1);
this.HasRequired(t => t.cat2)
.WithMany(t => t.posts)
.HasForeignKey(d => d.excat2);
this.HasRequired(t => t.cat3)
.WithMany(t => t.posts)
.HasForeignKey(d => d.excat3);
You need to add WillCascadeOnDelete(false) to the categories higher up the tree to your cat2Map and cat3Map.
cat2Map:
this.HasRequired(t => t.cat1)
.WithMany(t => t.cat2s)
.HasForeignKey(d => d.excat1)
.WillCascadeOnDelete(false);
cat3Map:
this.HasRequired(t => t.cat1)
.WithMany(t => t.cat3s)
.HasForeignKey(d => d.excat1)
.WillCascadeOnDelete(false); <-- Add this
this.HasRequired(t => t.cat2)
.WithMany(t => t.cat3s)
.HasForeignKey(d => d.excat2)
.WillCascadeOnDelete(false); <-- Add this

How do I use the 'When' method in ASP.NET MVC Fluent Validation?

I am trying to develop a fluent valiation rule where if my TitleId FK property is null, then the TitleOther text field becomes mandatory. I have tried several combinations and orders of fluent expressions, all to no avail.
This is what I have so far, if someone can please help me get this one When part correct, I would be most grateful, and a little more educated.
context.RulesFor(p => p.TitleId).Required(p => p.Message("Title is required."));
context.RulesFor(p => p.TitleOther)
.Required(p => p.Message("Please provide your other title."))
.Length(0, 50, c => c.Message("Other title may not exceed 50 characters")
.When(p => context.RulesFor(p => p.TitleId). *[what here?]*
I haven't used FluentValidation much, but from what I understand from the docs you would need to do
.When(p => p.TitleId == null)
instead of .When(p => context.RulesFor(p => p.TitleId). *[what here?]*
I have used When in my project in Fluent Validation. I have used for 1 condition that is compare password as like below:
RuleFor(u => u.Password).NotEmpty().WithMessage("Please Enter Password"); // Normal checked for non empty
When(u => !string.IsNullOrEmpty(u.Password) && !string.IsNullOrEmpty(u.ComfirmPassword), () =>
{
RuleFor(u => u.ComfirmPassword).Equal(x => x.Password).WithMessage("Password does not match");
}); // For compare password
Hope it helps you.
Thanks.

AutoMapper - MapFrom gives me the following error: expression tree cannot contain assignment operator

I've been using AutoMapper successfully for a while now. However, when I use MapFrom() in the CreateMap calls, I get the above error.
I have upgraded to version 2, and it seems the registries have been broken.
CreateMap<EmailInbound, EmailMessageModel>()
.ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.Subject = (string.IsNullOrEmpty(src.Subject) ? "No Subject" : src.Subject)));
Any ideas what has broken here?
You are trying to do an assignment in lambda expression instead of returning a value. Here's the fixed version.
CreateMap<EmailInbound, EmailMessageModel>()
.ForMember(dest => dest.Subject, opt => opt.MapFrom(src => string.IsNullOrEmpty(src.Subject)
? "No Subject"
: src.Subject));

Resources