Entity Framework Core
实体框架数据访问技术的轻量型、可扩展、开源、跨平台版本。
50 个问题
嗨,我在 Flight 和 Journey 之间有很多关系,所以我创建了一个中间表来创建关系:
问题是,我有一个航班列表和一个旅程,那么我如何创建代码来制作程序集,以便在使用实体框架保存它之后?
创建模型和上下文,此代码似乎没问题:
//process to create the Flight with the Transport list
var journey = new Journey()
{
JourneyOrigin = origin,
JourneyDestination = destination,
JourneyPrice = SumPrice
};
var response = new JourneyFlight()
{
Journey = journey,
Flight = flights[0]
};
Note:此问题总结整理于:How to save a many to many relationship with Entity Framework
欢迎来到 Microsoft Q&A,如果要在 ef core 中保存多对多关系,建议先使用代码生成模型,并在 OnModelCreating 事件中定义一些关系。
下面是一个可以参考的代码示例。
模型和 Dbcontext:
public class JourneyFlight
{
public int JourneyFlightId { get; set; }
public int JourneyId { get; set; }
public int FlightId { get; set; }
public Journey? journey { get; set; }
public Flight? flight { get; set; }
}
public class Journey
{
public int JourneyId { get; set; }
public double JourneyPrice { get; set; }
public ICollection<JourneyFlight>? journeyFlights { get; set; }
}
public class Flight
{
public int FlightId { get; set; }
public double FlightPrice { get; set;}
public int TransportId { get; set; }
public Transport? transport { get; set; }
public ICollection<JourneyFlight>? journeyFlights { get; set; }
}
public class Transport
{
public int TransportId { get; set; }
public int TransportFlightNumber { get; set; }
public ICollection<Flight>? flights;
}
public class MyContext:DbContext
{
public DbSet<JourneyFlight> journeyFlights { get; set; }
public DbSet<Journey> journeys { get; set; }
public DbSet<Flight> flights { get; set; }
public DbSet<Transport> transports { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connstr");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<JourneyFlight>().HasKey(m => new { m.JourneyId, m.FlightId });
modelBuilder.Entity<JourneyFlight>().HasOne(m => m.journey).WithMany(m => m.journeyFlights).HasForeignKey(m=>m.JourneyId);
modelBuilder.Entity<JourneyFlight>().HasOne(m => m.flight).WithMany(m => m.journeyFlights).HasForeignKey(m=>m.FlightId);
modelBuilder.Entity<Flight>().HasOne(m => m.transport).WithMany(m => m.flights).HasForeignKey(m => m.TransportId);
}
}
如何在main方法中添加数据:
MyContext context=new MyContext();
JourneyFlight journeyFlight1=new JourneyFlight();
JourneyFlight journeyFlight2 = new JourneyFlight();
var j1=new Journey();
j1.JourneyPrice = 100;
var j2=new Journey();
j2.JourneyPrice = 200;
var f1=new Flight();
f1.FlightPrice = 50;
var f2=new Flight();
f2.FlightPrice = 70;
journeyFlight1.journey = j1;
journeyFlight1.flight = f1;
journeyFlight2.journey = j2;
journeyFlight2.flight = f2;
Transport transport = new Transport();
transport.flights = new List<Flight>
{
f1,
f2
};
transport.TransportFlightNumber = 1001;
context.journeyFlights.Add(journeyFlight1);
context.journeyFlights.Add(journeyFlight2);
context.transports.Add(transport);
context.SaveChanges();
根据我的测试,我可以在sql server中得到以下结果:
希望我的代码示例能对您有所帮助。
如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。
注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。