gpt4 book ai didi

c# - 在 Entity Framework 中,我有一个 id (int) 用于收集其他类的 id。如何将它从类转换为id?

转载 作者:太空宇宙 更新时间:2023-11-03 16:03:07 25 4
gpt4 key购买 nike

以下信息,以-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 以保存到数据库中。如何做到这一点?

如果有不清楚的地方,请告诉我。先感谢您。

最佳答案

如果你不能/不愿模仿

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

关于c# - 在 Entity Framework 中,我有一个 id (int) 用于收集其他类的 id。如何将它从类转换为id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20511282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com