1

誰かがいくつかの指示を与えることができますか?まず、OrderテーブルとOrderItemテーブルの間に多対1の関係を作成し、ProductとOrderItemの間に多対1の関係を作成し、EF Code First Powertoolでコードを生成した後、これらのテーブル間の関係を削除しました。データベースの代わりにEFタイプが関係を維持し、データの整合性をチェックできることを期待していました。誰かが、独立した関連付けはデータベースに存在しない関係を作成できると言いました。しかし、私はそれを作ることができません。これが私のコードです。確認してください。ありがとう

public class Order
{
    public Order()
    {
        this.OrderItems = new List<OrderItem>();
    }

    public System.Guid Id { get; set; }
    public System.DateTime CreatedTime { get; set; }
    public virtual ICollection<OrderItem> OrderItems { get; set; }
}

public class OrderItem
{
    public System.Guid Id { get; set; }
    public Nullable<System.Guid> OrderId { get; set; }
    public Nullable<System.Guid> ProductId { get; set; }
    public Nullable<System.DateTime> CreatedTime { get; set; }
    public virtual Order Order { get; set; }
    public virtual Product Product { get; set; }
}

public class Product
{
    public Product()
    {
        this.OrderItems = new List<OrderItem>();
    }

    public System.Guid Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<OrderItem> OrderItems { get; set; }
}

public class OrderItemMap : EntityTypeConfiguration<OrderItem>
{
    public OrderItemMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        // Table & Column Mappings
        this.ToTable("OrderItem");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.OrderId).HasColumnName("OrderId");
        this.Property(t => t.ProductId).HasColumnName("ProductId");
        this.Property(t => t.CreatedTime).HasColumnName("CreatedTime");

        // Relationships
        this.HasRequired(t => t.Order)

            .WithMany(t => t.OrderItems)
            .HasForeignKey(d => d.OrderId);
        this.HasRequired(t => t.Product)
            .WithMany(t => t.OrderItems)
            .HasForeignKey(d => d.ProductId);
     }
}

public class OrderMap : EntityTypeConfiguration<Order>
{
    public OrderMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        // Table & Column Mappings
        this.ToTable("Order");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.CreatedTime).HasColumnName("CreatedTime");
    }
}

public class ProductMap : EntityTypeConfiguration<Product>
{
    public ProductMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Name)
            .IsFixedLength()
            .HasMaxLength(100);

        // Table & Column Mappings
        this.ToTable("Product");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.Name).HasColumnName("Name");
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (var context = new ProductsContext())
        {
            Guid gProductId = Guid.NewGuid();
            Guid gOrderId = Guid.NewGuid();
            Product product = new Product
            {
                Id = gProductId,
                Name = "Prodcut1",
            };
            Order order = new Order()
            {
                Id = gOrderId,
                CreatedTime = DateTime.Now
            };
            Guid gItem1Id = Guid.NewGuid();

            OrderItem item1 = new OrderItem()
            {
                Id = gItem1Id,
                 Order=order,
                 Product=product,
                CreatedTime = DateTime.Now
            };
            context.Orders.Add(order);
            context.Products.Add(product);
            context.OrderItems.Add(item1);
            context.SaveChanges();//this one works
        }
        Console.WriteLine("Created now...");
        Console.ReadLine();

        using (var c = new ProductsContext())
        {
            Guid gItem2Id = Guid.NewGuid();
            OrderItem item2 = new OrderItem()
            {
                Id = gItem2Id,
                OrderId = Guid.NewGuid(),//doesn't exist order , 
                CreatedTime = DateTime.Now
            };

            c.OrderItems.Add(item2);
            c.SaveChanges();
        }
    }
}

SaveChanges最後は「注文が存在しない、このレコードを挿入できません」という例外をスローすることを期待していました。しかし、そうではありません。このレコードは正常に作成されました。

4

1 に答える 1

1

エンティティ フレームワークは、完全な参照整合性を強制しません。いくつかの基本的なシナリオのみを適用します。たとえば、子エンティティが存在する場合は親エンティティを削除できませんが、親と子の両方がコンテキストに読み込まれている場合にのみ削除できます。完全な参照整合性は、データベース (または独自のカスタム ロジック) の責任です。

于 2012-11-26T12:44:02.143 に答える