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 7718831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:15:50+00:00 2026-06-01T03:15:50+00:00

my code like below public class User { public int ID { get; set;

  • 0

my code like below

public class User
{
    public int ID { get; set; }
    public int BillingAddressID { get; set; }
    public Address BillingAddress { get; set; }
    public IList<Shipment> Shipments { get; set; }
}

public class Address
{
    public int ID { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
}

public class Shipment
{
    public int ID { get; set; }
    public string State { get; set; }
    public int DeliveryAddressID { get; set; }
    public Address DeliveryAddress { get; set; }
    public User ShipUser { get; set; }
    //[ForeignKey("ShipUser")]
    public int ShipUserID { get; set; }
    //public int UserId { get; set; }
}

public class TestContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Address> Addresses { get; set; }
    public DbSet<Shipment> Shipments { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Shipment>().HasRequired(u => u.ShipUser)
            .WithMany(d => d.Shipments)
            .HasForeignKey(c => c.ShipUserID)
            .WillCascadeOnDelete(false);
    }
}
  1. if i remove the override method,i will get an error “SqlException: Introducing FOREIGN KEY constraint ‘FK_Shipments_Users_ShipUserID’ on table ‘Shipments’ may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
    Could not create constraint. See previous errors.”
  2. if i remove ShipUserID in Shipment Class,it will work ok,when i see the table that is created by ef,i found a column named Shipment_UserID in table Shipment.I don`t know why.
  3. if rename the class indenty key to UserID,it also work ok.

I try it anyway,but I don`t know the reason, I need some books about EF associations.

  • 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-01T03:15:52+00:00Added an answer on June 1, 2026 at 3:15 am
    1. If you don’t have mapping specified without cascadeDelete=false for one relationship it will create multiple cascade paths if you have tow relationships to user from Shipment.
      By convention you can use public


      Public User ShipUser { get; set; }
      public int ShipUserID { get; set; }

    it will use ShipUserID as foreign key by convention.

    1. If you remove ShipUserID Ef need to create his own foreign key to keep the relationship . that is your ‘ Shipment_UserID’

    2. rename the class indenty key to UserID I don’t understand what you meant.

    Here is a good tutorial to start with

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

Sidebar

Related Questions

I am working with NHibernate and I get this code below: public class User
The code looks like below: namespace Test { public interface IMyClass { List<IMyClass> GetList();
I met an interesting issue about C#. I have code like below. List<Func<int>> actions
I have a class as below: public class Node { public int NodeID {
Let's say there is a entity class like this @Entity public class User {
The code below give generate the ListView public class MyList extends ListActivity { static
I have a class ComponentItem which i simplified (pseudo code) below public class ComponentItem
This has been bothering me lately- when I use some code like below to
I have a code like this below, which gives me a $link that equals
I constantly find myself writing similar code like the example below: if (object[Object Name]

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.