get; set; }
public string Name { get; set; }
}
public class Transaction
{
public int Id { get; set; }
public string TransactionMesage { get; set; }
public int SenderId { get; set; }
public Person? Sender { get; set; }
public int RecipientId { get; set; }
public Person? Recipient { get; set; }
}
public class ApplicationContext : DbContext
{
public ApplicationContext()
{
Database.EnsureDeleted();
Database.EnsureCreated();
}
public DbSet<Person> Persons { get; set; } = null!;
public DbSet<Transaction> Transactions { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=datebase.db");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Transaction>(c =>
{
c.HasOne(x => x.Recipient).WithMany().HasForeignKey(x => x.RecipientId).OnDelete(DeleteBehavior.Restrict);
c.HasOne(x => x.Sender).WithMany().HasForeignKey(x => x.SenderId).OnDelete(DeleteBehavior.Restrict);
});
Person dima = new Person { Id = 1, Name = "Дима" };
Person rus = new Person { Id = 2, Name = "Рус" };
modelBuilder.Entity<Person>().HasData(dima, rus);
modelBuilder.Entity<Transaction>().HasData(new Transaction
{
Id = 1,
TransactionMesage = "Hello Word",
SenderId = dima.Id,
RecipientId = rus.Id,
});
}
}
заработало
логично)
Обсуждают сегодня