Learn notes 33_ef cross-database

Source: Internet
Author: User

In app. Config, you can:

(1) Custom classes

Public Xxxxdbcontext ()

{

Public Xxxxdbcontext (): Base ("Name=xxxxcontainer")//This is something that points to its connection string

{

    

}
protected overrride void onmodelcreating (Dbmodelbuilder modelBuilder)

{

}

Pulbic dbset<model1> Model1{get;set;}

Public dbset<model2> Model2 {get;set;}

}

(2) Model1 and Model2 write their own

public class Model2

{

[Key]

public int id{get;set;}

public virtual Model1 Model1{get;set;}

}

(3) Add EF related references, note that in the new version of VS, the referenced assemblies are placed in the Packages folder under the project

(4)

<configuration>

<connectionStrings>

<add name= "Xxxxcontainer" connectionString = "Server=localhost;userid =root;password=123;persist Security Info = True;database=dbname;charset=utf8; "

Providername= "Mysql.Data.MysqlClient" >//if it is SQL Server, then System.Data.SqlClient

</...>

</...>

(5) Write code

* CREATE DATABASE dynamically

DbContext.Database.CreateIfNotExists ();

//

Learn notes 33_ef cross-database

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.