Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8902753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:41:37+00:00 2026-06-15T01:41:37+00:00

Can anyone give some instructions? Firstly, I created many-to-one relationship between Order and OrderItem

  • 0

Can anyone give some instructions?
Firstly, I created many-to-one relationship between Order and OrderItem tables, and many-to-one relationship between Product and OrderItem, then I just deleted relationship between these tables after generating the code with EF Code First Powertool. I expected EF types instead of database can keep the relationship and check the Data consistency . Someone said independent association can create relationships that don’t exist in the database. But I can not make it. here is my code , please review it . thanks

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();
        }
    }
}

I expected the last SaveChanges would throw an exception saying “order doesn’t exist, can not insert this record”. But it doesn’t. This record was created successfully.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:41:38+00:00Added an answer on June 15, 2026 at 1:41 am

    Entity framework doesn’t enforce full referential integrity. It enforces only some basic scenarios – for example it will not allow you to delete parent entity if child entity exists but only if both parent and child are loaded in your context. The full referential integrity is responsibility of the database (or your own custom logic).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can anyone give me some tips or hook me up with some good links
Can anyone give me some idea about uploading data from excel to access database
I can't understand what the below ruby code does. Can anyone give me some
Can anyone give some clues as to why when I try to render the
Can anyone give some sample program in Lex and Yacc. Thanks.
http://lv.php.net/manual/en/function.exif-imagetype.php Can anyone give some more information about the rest extension types. About these:
Can anyone give me some information on consuming a WCF service from ILERPG? I
Can anyone give me some idea abt how to page a HTML page? The
can anyone give me some steps to create DLL without using class which means
Can anyone give out some example code which will make malloc signal an sigsegv?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.