Entity framework 6 属性在DB中创建两个副本,一个带有FK,另一个不带FK

Entity framework 6 属性在DB中创建两个副本,一个带有FK,另一个不带FK,entity-framework-6,Entity Framework 6,dotnetframework4.6.1 环境足迹6 C# 我正在使用一个API,我收到的JSON在一个对象中包含一个数组 我有强类型的类模型来处理这个输出。包装器类是EiaFuelIndexSeries。由于JSON反序列化程序,对于数组数组,我为反序列化程序定义了一个名为“data”的List>属性,并且我有一个[NotMapped]属性,因此EF6会忽略它 “data”属性包含字符串、字符串对的列表(但实际上是字符串、单个) 我创建了一个名为“FuelIndex”的新属性,其类型为List

dotnetframework4.6.1 环境足迹6 C#

我正在使用一个API,我收到的JSON在一个对象中包含一个数组

我有强类型的类模型来处理这个输出。包装器类是EiaFuelIndexSeries。由于JSON反序列化程序,对于数组数组,我为反序列化程序定义了一个名为“data”的List>属性,并且我有一个[NotMapped]属性,因此EF6会忽略它

“data”属性包含字符串、字符串对的列表(但实际上是字符串、单个)

我创建了一个名为“FuelIndex”的新属性,其类型为List,它将列表>转换为List

我看到的情况是,当EF尝试保存此文件时,它会创建一个版本的List,而不使用EiaFuelIndexSeries的GUID FK,然后创建一个版本,其中FK来自EiaFuelIndexSeries。可能是因为EF调用此属性两次,第一次未创建GUID PK,但第二次创建GUID PK

处理这个问题的策略是什么

包含此属性的“父”类是:

public class EiaFuelIndexSeries
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public Guid Id { get; set; }


// JSON of an element that is an array of arrays gets deserialized to this
        [NotMapped]
        public List<List<string>> data { get; set; }

// This part here takes the <List<List<string>> and creates a list of a type that better represents "data"

        private List<EiaFuelIndex> fuelIndices;
        public List<EiaFuelIndex> FuelIndices
        {
            get
            {
                fuelIndices = new List<EiaFuelIndex>();
                foreach(List<string> stringList in data)
                {
                    EiaFuelIndex fi = new EiaFuelIndex();
                     
                    if(stringList.Count == 2)
                    {
                        fi.IndexDate = stringList[0];
                        Single indexValue;

                        if(float.TryParse(stringList[1], out indexValue))
                        {
                            fi.IndexValue = indexValue;
                            fuelIndices.Add(fi);
                        }
                    }


                }
                return fuelIndices;
            }

}
这就是导入数据库的内容

public class EiaFuelIndex
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public Guid Id { get; set; }

        public string IndexDate{get;set;}
        public Single IndexValue{get;set;}
}