在实体框架中,我有一个id(int),用于收集其他类的id.如何将其从类转换为id

本文关键字:id 其他 转换 用于 框架 实体 有一个 int | 更新日期: 2024-09-08 04:22:49

以下信息,以-Values结尾的类是要保存到数据库的实体。以-Parameter结尾的是用于业务逻辑计算的类。

public class SampleValues // this is a entity that i want to collect the deatil id
{
    public int Id { get; set; }
    public int[] SampleDetailIdValue { get; set; }
}
public class SampleDetailValues // this is the detail entity
{
    public int Id { get; set; }
}
public class SampleParameter // this is a class that i will use for the business logic
{
    public string Name { get; set; }
    public SampleDetailValues[] SampleDetail { get; set; }
    public double { get; set; }
}
public class SampleDetailParameter
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public double Value { get; set; }
}

问题是我想转换SampleDetailValues[]中的id以保存到数据库中。如何做到这一点?

如果有不清楚的地方,请告诉我。提前谢谢。

在实体框架中,我有一个id(int),用于收集其他类的id.如何将其从类转换为id

如果您不能/不会作为建模

public class SampleDetailValues // this is the detail entity
{
   public int Id { get; set; }
}
// typically done with a join table, since arrays cant be modelled to scalar DB types
public class SampleParameterIds
{
    public int Id { get; set; }
    public string SampleParameterName {get;set}
    public int DetailId { get; set; } 
    [ForeignKey("DetailId")]
    public virtual SampleDetailValues detailValue {get; Set;}          
    [ForeignKey("SampleParamaterName")]
    public virtual SampleParamter sampleParameter {get; Set;}          
}
public class SampleParameter // this is a class that i will use for the business logic
{
    public string Name { get; set; }
//   public SampleDetailValues[] SampleDetail { get; set; } // see join table 
    public double { get; set; }
}

然后尝试使用具有正确值的"管理"字符串。可以对数组进行CCD_ 1-ed以生成字符串并进行拆分以填充数组。但是你而不是EF必须管理内容

public class SampleDetailValues // this is the detail entity
{
   public int Id { get; set; }
}

public class SampleParameter // this is a class that i will use for the business logic
{
    public string Name { get; set; }
    public string SampleDetailIds {get;Set;} // you should convert array to string and back...eg join/split
    public SampleDetailValues[] SampleDetail { get; set; } // ef should ignore this type as it is invalid on Db.
    public double { get; set; }
}