Entity Framework Code First Fluent API configuration for one to one identifying relationship
Date : March 29 2020, 07:55 AM
this one helps. The way EF implements one-to-one is to make the dependent entity have a primary key that's also a foreign key to the principle entity. So the dependent's PK is naturally constrained to existing principle PK values. So using your classes, slightly modified: public class Customer
{
public int CustomerId { get; private set; }
public virtual Card Card { get; set; }
}
public abstract class Card
{
public int CustomerId { get; private set; }
}
public class Visa : Card { }
public class Amex : Card { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().HasRequired(c => c.Card)
.WithRequiredPrincipal();
modelBuilder.Entity<Card>().HasKey(c => c.CustomerId);
}
using (var db = new TempModelsContext())
{
var cst = new Customer { Name = "Customer1",
Card = new Amex { Number = "Amex" } };
db.Customers.Add(cst);
db.SaveChanges();
}
using (var db = new TempModelsContext())
{
var cst = db.Customers.Include(c => c.Card).Single(c => c.CustomerId == 1);
cst.Card = new Visa { Number = "Visa" };
db.SaveChanges();
}
var cst = db.Customers.Include(c => c.Card).Single(c => c.CustomerId == 1);
db.Cards.Remove(cst.Card);
db.SaveChanges();
cst.Card = new Visa { Number = "Visa" };
db.SaveChanges();
|
Fluent configuration for base entity in Entity Framework
Tag : chash , By : obijywk
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Do I really have to configure the MaxLength of the two string properties in every single DbModelBuilder configuration? protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<BaseEntity>().Property(x => x.CreateUserId).HasMaxLength(36);
modelBuilder.Entity<BaseEntity>().Property(x => x.ModifyUserId).HasMaxLength(36);
base.OnModelCreating(modelBuilder);
}
modelBuilder.Properties().Where(x => x.Name == "CreateUserId").Configure(x => x.HasMaxLength(36));
modelBuilder.Properties().Where(x => x.Name == "ModifyUserId").Configure(x => x.HasMaxLength(36));
|
How to use use same configuration between .NET Framework, .NET Standard, and .NET Core project?
Date : March 29 2020, 07:55 AM
I wish this help you You should read the value from configuration before calling your library, and pass it by parameter, either into a method or constructor. In general, library code shouldn't depend on side effects from the configuration of its host environment. Doing so makes it very difficult to reuse code, or to test effectively.
|
Port existing code first models, and fluent aPI configuration from Entity Framework 6 to Entity Framework Core 2.0
Tag : chash , By : Anonymous
Date : March 29 2020, 07:55 AM
around this issue In EF Core 2.0 the Car model changes because of the need to specify the CarOwner foreign key in the Model as a nullable integer property public class CarConfiguration : IEntityTypeConfiguration<Car>
{
public void Configure(EntityTypeBuilder<Car> builder)
{
builder.HasKey(a => a.Id);
builder.Property(a => a.Id).ValueGeneratedOnAdd().IsRequired();
builder.HasOne(x => x.CarOwner)
.WithMany(x => x.Cars)
.HasForeignKey("CarOwner_Id") // <--
.IsRequired(false);
}
}
public class CarOwnerConfiguration : IEntityTypeConfiguration<CarOwner>
{
public void Configure(EntityTypeBuilder<CarOwner> builder)
{
builder.HasKey(a => a.Id);
builder.Property(a => a.Id).ValueGeneratedOnAdd().IsRequired();
}
}
modelBuilder.Entity<Car>()
.HasOne(x => x.CarOwner)
.WithMany(x => x.Cars)
.HasForeignKey("CarOwner_Id");
|
Cannot use Entity Framework in .NET standard 2 project
Tag : chash , By : Murali Ravipudi
Date : March 29 2020, 07:55 AM
it helps some times It looks like the actual question is "How can I create models and reverse engineer a database using EF Core like I could with EF 6" ? The tooling is separate from the NuGet package. Visual Studio 2017 doesn't include modelling or reverse engineer tools for EF Core. Those are available through EF Core Power Tools an open source addon. You can find the code and documentation here
|