1

レガシー システムからの 2 つのテーブルがあります。これらのテーブルは、2 つの別個の外部ソースから定期的に更新され、アプリケーションでデータを参照するための単なる「読み取り専用」テーブルとして使用されます。

配送先

public partial class DeliverySite
{
    public string CustomerID { get; set; } // PK
    public string CustomerName { get; set; }
    public string DeliveryAddress { get; set; }
    public string BillingAddress { get; set; }
    //... fields removed for clarity.

    // Navigational Properties.
    public virtual ICollection<Item> Items { get; set; }
}

public class DeliverySiteMap : EntityTypeConfiguration<DeliverySite>
{
    public DeliverySiteMap()
    {
        // Primary Key
        this.HasKey(t => t.CustomerID);

        // Properties
        this.Property(t => t.CustomerID)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.CustomerName)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.DeliveryAddress)
            .IsRequired();

        this.Property(t => t.BillingAddress)
            .IsRequired();

        // Table & Column Mappings
        this.ToTable("DeliverySites");
        this.Property(t => t.CustomerID).HasColumnName("CustomerID");
        this.Property(t => t.CustomerName).HasColumnName("CustomerName");
        this.Property(t => t.DeliveryAddress).HasColumnName("DeliveryAddress");
        this.Property(t => t.BillingAddress).HasColumnName("BillingAddress");   
    }
}

アイテム

public partial class Item
{
    public string Item { get; set; }  // PK
    public string ItemDescription { get; set; }
    public decimal Brand { get; set; }
    public decimal Price { get; set; }
    public string CustomerID { get; set; }  // PK + FK
    //... fields removed for clarity.

    // Navigational Properties.
    public virtual DeliverySite DeliverySite { get; set; }
}

public class ItemMap : EntityTypeConfiguration<Item>
{
    public ItemMap()
    {
        // Primary Key
        this.HasKey(t => new { t.Item, t.CustomerID });

        // Properties
        this.Property(t => t.UserItem)
            .HasMaxLength(50);

        this.Property(t => t.UserItemDescription)
            .HasMaxLength(255);

        this.Property(t => t.CCItem)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.CCItemDescription)
            .IsRequired()
            .HasMaxLength(255);

        this.Property(t => t.CustomerID)
            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("Items");
        this.Property(t => t.Item).HasColumnName("Item");
        this.Property(t => t.ItemDescription).HasColumnName("ItemDescription");
        this.Property(t => t.Brand).HasColumnName("Brand");
        this.Property(t => t.Price).HasColumnName("Price");
        this.Property(t => t.CustomerID).HasColumnName("CustomerID");
    }
}

これらのテーブルは個別に更新されるため、"DeliverySites" に存在しない "Items" が入力されている可能性があります。

したがって、オプションの関係を作成したいと思います。(したがって、アプリケーション内でナビゲーション プロパティを利用できますが、テーブルが個別に更新されるのを妨げないようにします。)

私の中で私ItemMap : EntityTypeConfiguration<Item>は次のことを試しました:

this.HasOptional(x => x.DeliverySite)
    .WithMany(x => x.Items)
    .HasForeignKey(x => x.CustomerID)
    .WillCascadeOnDelete(false);

しかし、私はこのエラーが発生します:

System.Data.Entity.Edm.EdmAssociationType: : 多重度は、関係 'Item_DeliverySite' のロール 'Item_DeliverySite_Target' の参照制約と競合します。従属ロールのすべてのプロパティは null 非許容であるため、プリンシパル ロールの多重度は '1' でなければなりません。

この関係をどのように実装する必要がありますか?

また、データベースに FK 制約を追加せずにこれを行うことができれば理想的です。これは可能ですか?

4

3 に答える 3

0

外部キーを削除して作成すると、これが機能すると思います。

this.HasOptional(x => x.DeliverySite)
    .WithMany(x => x.Items);
于 2013-06-11T12:11:14.463 に答える