0

EFデータベースにデータを追加するために使用したい次のコードがあります。

        var applicationNames = new[] { 
            "C", 
            "E", 
            "S" };
        var testAccountNames = new[] {
            "Production",
            "Ready",
            "Beta",
            "Alpha" };
        foreach (string applicationName in applicationNames)
        {
            _uow.Applications.Add(
                new Application 
                { 
                    Name = applicationName, 
                    ModifiedDate = DateTime.Now 
                });
                foreach (string testAccountName in testAccountNames)
                {
                   new TestAccount 
                   { 
                       ApplicationID = ??
                       Name = applicationName, 
                       ModifiedDate = DateTime.Now 
                   });
                }
        }
        _uow.Commit();

これが私が持っているクラスです:

public partial class Application
{
    public Application()
    {
        this.TestAccounts = new List<TestAccount>();
    }

    public int ApplicationId { get; set; }
    public string Name { get; set; }
    public byte[] RowVersion { get; set; }
    public System.DateTime ModifiedDate { get; set; }
    public virtual ICollection<TestAccount> TestAccounts { get; set; }
}

public partial class TestAccount
{
    public TestAccount()
    {
        this.Subjects = new List<Subject>();
    }

    public int TestAccountId { get; set; }
    public string Name { get; set; }
    public int ApplicationId { get; set; }
    public byte[] RowVersion { get; set; }
    public System.DateTime ModifiedDate { get; set; }
    public virtual Application Application { get; set; }
    public virtual ICollection<Subject> Subjects { get; set; }
}

addメソッドに使用するコードは次のとおりです

    public virtual void Add(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            dbEntityEntry.State = EntityState.Added;
        }
        else
        {
            DbSet.Add(entity);
        }
    }

マッピングは次のとおりです。

    public TestAccountMap()
    {
        // Primary Key
        this.HasKey(t => t.TestAccountId);

        // Relationships
        this.HasRequired(t => t.Application)
            .WithMany(t => t.TestAccounts)
            .HasForeignKey(d => d.ApplicationId);

    }

データベースでは、ApplicationIdとTestIdはIDデータ型であることに注意してください。また、ApplicationテーブルのApplicationIDをTestAccountテーブルのApplicationIdに正しくリンクする外部キーもあります。

EFがデータベースに挿入するときに、TestAccountテーブルの正しいApplicationIDにデータを挿入するようにするにはどうすればよいですか?

4

2 に答える 2

0
foreach (string applicationName in applicationNames)
        {
            var app = new Application 
                { 
                    Name = applicationName, 
                    ModifiedDate = DateTime.Now 
                });
            _uow.Applications.Add(app);
                foreach (string testAccountName in testAccountNames)
                {
                   new TestAccount 
                   { 
                       Application = app ,
                       Name = applicationName, 
                       ModifiedDate = DateTime.Now 
                   });
                }
        }
于 2013-03-25T12:54:59.810 に答える
0

IDを直接設定せず、参照プロパティを設定します。EFが残りを分類するため、次のようになります。

....
foreach (string applicationName in applicationNames)
    {
        _uow.Applications.Add(
            new Application 
            { 
                Name = applicationName, 
                ModifiedDate = DateTime.Now,
                TestAccounts = (from  testAccountName in testAccountNames
                                select new TestAccount
                                { 
                                   Name = testAccountName , 
                                   ModifiedDate = DateTime.Now 
                               })
            }
    }
于 2013-03-25T13:01:58.907 に答える