[英]Entity Framework Database Model
Dear Entity experts and others, 尊敬的实体专家和其他人士,
I have the following entities, 我有以下实体,
Base class Individual: 基类个人:
public abstract class Individual
{
[Key]
public int IndividualID { get; set; }
... other properties
}
Yogi: 瑜伽士:
[Table("Yogis")]
public class Yogi : Individual
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public DateTime Birthdate { get; set; }
}
Customer: 顾客:
[Table("Customers")]
public class Customer : Individual
{
public string Name { get; set; }
[ForeignKey("VATID")] // tried with and without
public virtual VAT VAT { get; set; }
}
VAT: 增值税:
public class VAT
{
[Key]
public int VATID { get; set; }
[Required]
public virtual Customer VATHolder { get; set; }
... other properties
}
DbContext: DbContext:
public DbSet<Individual> Individuals { get; set; }
public DbSet<VAT> VATS { get; set; }
So the above creates a table VAT where there VATID is 2 to start with which should be 1 instead, I don't understand why this happens, this should be 1, isn't it? 所以上面创建了一个表VAT,其中VATID是2,而VATID应该是1,我不明白为什么会发生这种情况,应该是1,不是吗? Also, there is no reference anywhere in any table that references Customer & VAT, how can I create a separate table VAT_Customer where you have a VATID & CustomerID ?
另外,在任何表中没有任何引用Customer&VAT的引用,如何在您具有VATID和CustomerID的地方创建单独的表VAT_Customer?
This is how i create and insert the entity Customer: 这是我创建和插入实体客户的方式:
VAT vat = new VAT("123456789");
DataBaseHandler.InsertIndividual(new Customer("Customer name", vat,...));
Insert Function: 插入功能:
public static void InsertIndividual(Individual individual)
{
using (MyDbContext ctx = new MyDbContext())
{
ctx.Individuals.Add(individual);
ctx.SaveChanges();
}
}
I have tried several ways ( EXAMPLES HERE ) but I prefer the TPT way, having a separate view of any entity or relations between entities. 我尝试了几种方法(在此处为示例 ),但我更喜欢TPT方法,对任何实体或实体之间的关系有单独的看法。 What am I doing wrong and how would I achieve the desired result?
我在做错什么,我将如何获得预期的结果? Let me know if I can clarify anything.
让我知道是否可以澄清任何事情。 Thank you in advance for any help or suggestions!
预先感谢您的任何帮助或建议!
Kind regards! 亲切的问候!
Your ForeignKey placement should look something like this if you wanna have 1:1 Relationship : 如果您想建立1:1关系,那么ForeignKey的位置应如下所示:
enter code here
[ForeignKey("VAT")]
public virtual int VATId { get; set; }
public virtual VAT VAT { get; set; }
Name in the ForeignKey is same as your class. ForeignKey中的名称与您的班级相同。
hope this can help 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.