手动创建实体具有外键的实体列表

时间:2013-04-26 10:17:50

标签: c# asp.net-mvc-3 entity-framework

我正在开发一个asp.net mvc 3应用程序。有一个数据库但是现在我不能向它添加数据,但是项目仍然首先使用Entity Framework和Database,所以我在我的解决方案中创建了实体。我想要的是创建一个实体列表,它应该表示一旦可以免费使用就可以查询数据库的结果。

这是由EF创建的实体:

    namespace DataAccess
    {
        using System;
        using System.Collections.Generic;

        public partial class MCS_DocumentFields
        {
            public long Id { get; set; }
            public int RowNo { get; set; }
            public int ColumnNo { get; set; }
            public Nullable<bool> IsRequired { get; set; }
            public string QuestionText { get; set; }
            public string FieldValue { get; set; }

            public virtual MCS_ContentTypes MCS_ContentTypes { get; set; }
            public virtual MCS_Documents MCS_Documents { get; set; }
            public virtual MCS_Fields MCS_Fields { get; set; }
        }
    }

I have Extensions so this is the extension for this entity :

namespace DataAccess
{
    [MetadataType(typeof(MCS_DocumentFieldsMetaData))]
    public partial class MCS_DocumentFields : BaseEntity
    {

    }

    internal sealed class MCS_DocumentFieldsMetaData
    {
        [Required]
        public int RowNo { get; set; }

        [Required]
        public int ColumnNo { get; set; }
    }
}

最后在我的控制器中,我创建了一个包含MCS_DocumentFields

实例的List
 List<MCS_DocumentFields> DocumentData = new List<MCS_DocumentFields>()
            {
                new MCS_DocumentFields {Id = 1, RowNo = 1, ColumnNo = 1, IsRequired = true, QuestionText = "alabala", FieldValue = ""},
                new MCS_DocumentFields {Id = 2, RowNo = 1, ColumnNo = 2, IsRequired = true, QuestionText = "", FieldValue = "yes"},
                new MCS_DocumentFields {Id = 3, RowNo = 2, ColumnNo = 1, IsRequired = true, QuestionText = "alabala", FieldValue = "yes"},
                new MCS_DocumentFields {Id = 4, RowNo = 3, ColumnNo = 1, IsRequired = true, QuestionText = "alabala", FieldValue = "yes"},
                new MCS_DocumentFields {Id = 5, RowNo = 4, ColumnNo = 1, IsRequired = false, QuestionText = "alabala", FieldValue = "no"},
                new MCS_DocumentFields {Id = 6, RowNo = 4, ColumnNo = 2, IsRequired = true, QuestionText = "alabala", FieldValue = "no"},
            }

问题是 - public virtual MCS_Fields MCS_Fields { get; set; }代表数据库中的FiedlId FK,我需要在DocumentData中为每条记录添加值。但是我无法直接使用FieldId,而是可以使用实体MCS_Fields,但我不知道如何从那里开始。显然只是输入MCS_Fields.Id不起作用......所以如何在我的列表中模拟这个?

0 个答案:

没有答案