简体   繁体   English

使用 dapper 扩展的外键和数据透视表

[英]Foreign keys and pivot tables using dapper extensions

I am using dapper extension methods like insert for everything related to the database in a project but I am not able to connect the models to each other.我正在对项目中与数据库相关的所有内容使用 dapper 扩展方法,例如插入,但我无法将模型相互连接。 There are 4 tables in the database: category, event, speaker, and eventspeaker (pivot table).数据库中有 4 个表:category、event、speaker 和 eventspeaker(数据透视表)。 Is there a way to do it like using classmappings in NHibernate?有没有办法像在 NHibernate 中使用类映射一样? Or do I have to change all foreign keys to int and do everything in the Save, update, etc.. methods?或者我是否必须将所有外键更改为 int 并在 Save、update 等方法中执行所有操作?

public class Event
{
  public int Id {get; set;}
  public Category Category {get;set;}
  public string Location {get;set;}
  public DateTime Time {get;set;}

  public void Save(){ /*TODO*/}
 }      

public class Category
{
  public int Id {get; set;}
  public string Category {get;set;}
  public void Save(){ /*TODO*/}
 }      

public class Speaker
{
  public int Id {get; set;}
  public string Name {get;set;}

  public void Save(){ /*TODO*/}
 }      

public class EventSpeaker
{
  public Event event {get; set;}
  public Speaker Speaker {get;set;}
 } 

There is no solution for pivot tables yet in DapperExtensions. DapperExtensions 中还没有数据透视表的解决方案。

Foreign keys should be stored as integers.外键应存储为整数。 If say a category instance is required for each event, then a non-mapped category instance can also be added.如果说每个事件都需要一个类别实例,那么还可以添加一个非映射的类别实例。

A class should also be added for each pivot table.还应该为每个数据透视表添加一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM