Some things to note about EF Code First MYSQL master-slave table design

Source: Internet
Author: User

If you have the following two sheets

public class Main

{

public int id{get;set;}

public string Name{get;set};

public virtual icollection<detail> Details{get;set;}

}

public class Detail

{

public int id{get;set;}

public int mainid{get;set;}

public string Desc{get;set;}

[ForeignKey ("Mainid")]

public virtual Main Main{get;set;}

}

Public dbset<main> Mains{get;set;}

Public dbset<detail> Details{get;set;}

Then in the Package Manager console input: update-database, the MySQL database successfully generated two tables.

One, update report, Table ' {database}.dbo. {TableName} ' doesn ' t exist

This time you find that you need to increase the cascade Delete and update features, OK, modify the detail,

public int mainid{get;set;}

Switch

[Required]

public int mainid{get;set;}

Then in Package Manager console input: Update-database-force

Find such errors: Table ' {database}.dbo. Details ' doesn ' t exist

The problem is in the DBO, where the solution is:

Add-migration-aa

A file such as 201710031324203_aa.cs will be generated with an up method in it, and the dbo will be removed and executed update-database-force will be done.

The possible reason is that the table described by EF when working with SQL Server is: DatabaseName.dbo.TableName is correct, and MySQL cannot have dbo (databasename.tablename), so there is an error.

Second, can not delete from the table

//public dbset<detail> Details{get;set;}

When you log out of details and execute Update-database-force, you will find that the table still exists in the database, with add-migration AA you will find that the content of the method up is empty.

This problem occurs because there is a reference to the table from the main table, which will

//public virtual icollection<detail> details{get;set;}

If you look at it, you can delete the detail table.

Some things to note about EF Code First MYSQL master-slave table design

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.