如何定义数据库中不存在的类型之间的多对一关系

时间:2012-11-25 13:02:15

标签: entity-framework-4

任何人都可以提供一些指示吗? 首先,我创建了Order和OrderItem表之间的多对一关系,以及Product和OrderItem之间的多对一关系,然后我在使用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会抛出异常,说“订单不存在,无法插入此记录”。但事实并非如此。此记录已成功创建。

1 个答案:

答案 0 :(得分:1)

实体框架不会强制实施完整的参照完整性。它仅强制执行一些基本方案 - 例如,如果子实体存在,则不允许您删除父实体,但仅当父元素和子元素都在您的上下文中加载时才允许。完整的引用完整性是数据库(或您自己的自定义逻辑)的责任。