在实体框架中,我有一个id(int)来收集另一个类的id。如何将它从类转换为id?

时间:2013-12-11 05:34:14

标签: c# entity-framework

以下信息,以-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转换为保存到数据库。怎么做?

如果不清楚,请告诉我。 先感谢您。

1 个答案:

答案 0 :(得分:0)

如果您不能/不建模

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; }
}

然后尝试 使用您使用正确值“管理”的字符串。数组可以join - 编辑一个字符串并拆分以填充数组。但是你和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; }
}