Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一对一/一对多_C#_Entity Framework_One To Many_One To One - Fatal编程技术网

C# 一对一/一对多

C# 一对一/一对多,c#,entity-framework,one-to-many,one-to-one,C#,Entity Framework,One To Many,One To One,我有一个与服务模型有一对一关系的事务模型,然后服务模型有许多ServiceAddon public class Transaction { public int TransactionId {get;set;} public string Username {get;set;} public Service OccasionService {get;set;} } public class Service { [Key] public int Transa

我有一个与服务模型有一对一关系的事务模型,然后服务模型有许多ServiceAddon

public class Transaction 
{
   public int TransactionId {get;set;}
   public string Username {get;set;}
   public Service OccasionService {get;set;}
}

public class Service 
{  
   [Key]
   public int TransactionId {get;set;}
   public Transaction Transaction { get; set; }
   public virtual IList<ServiceAddon> ServiceAddons { get; set; }
}

public class ServiceAddon
{
    public int ServiceAddonId { get; set; }
    public int TransactionId { get; set; }
    public int AddonId { get; set; }
    public decimal AddonPrice { get; set; }
    public virtual Addon Addon { get; set; }
}
公共类事务
{
public int TransactionId{get;set;}
公共字符串用户名{get;set;}
公共服务事件服务{get;set;}
}
公务舱服务
{  
[关键]
public int TransactionId{get;set;}
公共事务{get;set;}
公共虚拟IList ServiceAddons{get;set;}
}
公共类服务插件
{
公共int-ServiceAddonId{get;set;}
public int TransactionId{get;set;}
public int AddonId{get;set;}
公共十进制AddonPrice{get;set;}
公共虚拟加载项加载项{get;set;}
}

现在包管理器控制台告诉我,ServiceAddon中不存在OccasionServiceId,因此我更改了它的TransactionId,该程序已编译,但现在它显示无效列OccasionServiceId。如何修复此问题?

您需要指定
ForeignKey
s,请尝试类似的操作。我为不清楚的部分添加了注释

public class Transaction 
{
    [Key]
    public int TransactionId {get;set;}
    public string Username {get;set;}

    // Whatever the column is
    public int OccasionServiceId {get;set;}

    [ForeignKey("OccasionServiceId")] // What on Transaction points to Service's Key
    public Service OccasionService {get;set;}
}

public class Service 
{  
    // Ideally this would be called ServiceId instead?
    [Key]
    public int TransactionId {get;set;}

    // If this uses TransactionId then are you sure you don't need a Key that is ServiceId
    public Transaction Transaction { get; set; }

    [ForeignKey("TransactionId")] // What on ServiceAddon points at Service's Key
    public virtual ICollection<ServiceAddon> ServiceAddons { get; set; }
}

public class ServiceAddon
{
    [Key]
    public int ServiceAddonId { get; set; }

    // Don't you need a ServiceId here?

    public int TransactionId { get; set; }
    public int AddonId { get; set; }
    public decimal AddonPrice { get; set; }
    public virtual Addon Addon { get; set; }
}
公共类事务
{
[关键]
public int TransactionId{get;set;}
公共字符串用户名{get;set;}
//不管专栏是什么
public int-OccasionServiceId{get;set;}
[ForeignKey(“OccasionServiceId”)]//事务上的什么指向服务的密钥
公共服务事件服务{get;set;}
}
公务舱服务
{  
//理想情况下,这将被称为ServiceId?
[关键]
public int TransactionId{get;set;}
//如果使用TransactionId,那么您确定不需要ServiceId密钥吗
公共事务{get;set;}
[ForeignKey(“TransactionId”)]//ServiceAddon上的什么指向服务的密钥
公共虚拟ICollection服务插件{get;set;}
}
公共类服务插件
{
[关键]
公共int-ServiceAddonId{get;set;}
//这里不需要服务ID吗?
public int TransactionId{get;set;}
public int AddonId{get;set;}
公共十进制AddonPrice{get;set;}
公共虚拟加载项加载项{get;set;}
}
以下是我制作的模型的外观:

public class Transaction 
{
    [Key]
    public int TransactionId {get;set;}
    public string Username {get;set;}
    public int ServiceId {get;set;}

    [ForeignKey("ServiceId")]
    public Service Service {get;set;}
}

public class Service 
{  
    [Key]
    public int ServiceId {get;set;}
    public int TransactionId {get;set;}

    [ForeignKey("TransactionId")
    public Transaction Transaction { get; set; }

    [ForeignKey("TransactionId")] // What on ServiceAddon points at Service's Key
    public virtual ICollection<ServiceAddon> ServiceAddons { get; set; }
}

public class ServiceAddon
{
    [Key]
    public int ServiceAddonId { get; set; }
    public int ServiceId {get;set;}

    [ForeignKey("ServiceId")]
    public Service Service {get;set;}
}
公共类事务
{
[关键]
public int TransactionId{get;set;}
公共字符串用户名{get;set;}
public int ServiceId{get;set;}
[ForeignKey(“ServiceId”)]
公共服务服务{get;set;}
}
公务舱服务
{  
[关键]
public int ServiceId{get;set;}
public int TransactionId{get;set;}
[外键(“交易ID”)
公共事务{get;set;}
[ForeignKey(“TransactionId”)]//ServiceAddon上的什么指向服务的密钥
公共虚拟ICollection服务插件{get;set;}
}
公共类服务插件
{
[关键]
公共int-ServiceAddonId{get;set;}
public int ServiceId{get;set;}
[ForeignKey(“ServiceId”)]
公共服务服务{get;set;}
}

谢谢您的回答。我试过了,package manager说无法确定类型服务和事务之间的关联的主端?我想这可能是因为您的服务。TransactionId似乎既是外键又是主键?请参阅我的第二段代码。它应该类似于:
Transaction.TransactionId 1 1 Service.TransactionId
Service.ServiceId 1*ServiceAddon.ServiceId
我用
ServiceAddon
修改了一下答案。祝你好运!上床睡觉:实际上,我使用了你的第二个代码。删除数据库后,我现在就可以使用了。现在它告诉我ServiceAddons_Service_TransactionId有一个未解析的引用?你说得对,我需要那个服务Id。这就是我想要做的,我现在不知道为什么我把TransactionId放在那里。谢谢