Link models from different databases












0















I have the following situation: I have a class Company, it is as simple as possible



public class Company
{
public int ID { get; set; }

public string Name { get; set; }
}


I also need to extend the IdentityUser class from ASP. NET Identity Core:



public class User : IdentityUser
{
public Company Company { get; set; }

public int CompanyID { get; set; }
}


Not bad so far. But I also need a class Device :



public class Device
{
public Company Company { get; set; }

public int CompanyID { get; set; }
}


Accordingly, we add the Devices property of the List<Device> type to the Company class and Users property of the List<User> type.



The problem is that I want to separate the data (Device table) from the data of ASP .NET Identity Core in different databases. Table Company I want to put in the database with Auth data. And table Device in other one. How can I tell the Entity Framework that the Company property of the Device class is from another database?










share|improve this question

























  • So, Company has many Device, and Device has one Company?

    – Foo
    Nov 20 '18 at 3:36











  • Yes, you understood correctly

    – starmucks
    Nov 20 '18 at 3:38






  • 1





    How can I tell ... You can't. One context = one database.

    – Gert Arnold
    Nov 20 '18 at 21:25











  • Please read other answers and comments

    – starmucks
    Nov 21 '18 at 10:36
















0















I have the following situation: I have a class Company, it is as simple as possible



public class Company
{
public int ID { get; set; }

public string Name { get; set; }
}


I also need to extend the IdentityUser class from ASP. NET Identity Core:



public class User : IdentityUser
{
public Company Company { get; set; }

public int CompanyID { get; set; }
}


Not bad so far. But I also need a class Device :



public class Device
{
public Company Company { get; set; }

public int CompanyID { get; set; }
}


Accordingly, we add the Devices property of the List<Device> type to the Company class and Users property of the List<User> type.



The problem is that I want to separate the data (Device table) from the data of ASP .NET Identity Core in different databases. Table Company I want to put in the database with Auth data. And table Device in other one. How can I tell the Entity Framework that the Company property of the Device class is from another database?










share|improve this question

























  • So, Company has many Device, and Device has one Company?

    – Foo
    Nov 20 '18 at 3:36











  • Yes, you understood correctly

    – starmucks
    Nov 20 '18 at 3:38






  • 1





    How can I tell ... You can't. One context = one database.

    – Gert Arnold
    Nov 20 '18 at 21:25











  • Please read other answers and comments

    – starmucks
    Nov 21 '18 at 10:36














0












0








0








I have the following situation: I have a class Company, it is as simple as possible



public class Company
{
public int ID { get; set; }

public string Name { get; set; }
}


I also need to extend the IdentityUser class from ASP. NET Identity Core:



public class User : IdentityUser
{
public Company Company { get; set; }

public int CompanyID { get; set; }
}


Not bad so far. But I also need a class Device :



public class Device
{
public Company Company { get; set; }

public int CompanyID { get; set; }
}


Accordingly, we add the Devices property of the List<Device> type to the Company class and Users property of the List<User> type.



The problem is that I want to separate the data (Device table) from the data of ASP .NET Identity Core in different databases. Table Company I want to put in the database with Auth data. And table Device in other one. How can I tell the Entity Framework that the Company property of the Device class is from another database?










share|improve this question
















I have the following situation: I have a class Company, it is as simple as possible



public class Company
{
public int ID { get; set; }

public string Name { get; set; }
}


I also need to extend the IdentityUser class from ASP. NET Identity Core:



public class User : IdentityUser
{
public Company Company { get; set; }

public int CompanyID { get; set; }
}


Not bad so far. But I also need a class Device :



public class Device
{
public Company Company { get; set; }

public int CompanyID { get; set; }
}


Accordingly, we add the Devices property of the List<Device> type to the Company class and Users property of the List<User> type.



The problem is that I want to separate the data (Device table) from the data of ASP .NET Identity Core in different databases. Table Company I want to put in the database with Auth data. And table Device in other one. How can I tell the Entity Framework that the Company property of the Device class is from another database?







entity-framework asp.net-core ef-fluent-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:25







starmucks

















asked Nov 20 '18 at 3:31









starmucksstarmucks

287




287













  • So, Company has many Device, and Device has one Company?

    – Foo
    Nov 20 '18 at 3:36











  • Yes, you understood correctly

    – starmucks
    Nov 20 '18 at 3:38






  • 1





    How can I tell ... You can't. One context = one database.

    – Gert Arnold
    Nov 20 '18 at 21:25











  • Please read other answers and comments

    – starmucks
    Nov 21 '18 at 10:36



















  • So, Company has many Device, and Device has one Company?

    – Foo
    Nov 20 '18 at 3:36











  • Yes, you understood correctly

    – starmucks
    Nov 20 '18 at 3:38






  • 1





    How can I tell ... You can't. One context = one database.

    – Gert Arnold
    Nov 20 '18 at 21:25











  • Please read other answers and comments

    – starmucks
    Nov 21 '18 at 10:36

















So, Company has many Device, and Device has one Company?

– Foo
Nov 20 '18 at 3:36





So, Company has many Device, and Device has one Company?

– Foo
Nov 20 '18 at 3:36













Yes, you understood correctly

– starmucks
Nov 20 '18 at 3:38





Yes, you understood correctly

– starmucks
Nov 20 '18 at 3:38




1




1





How can I tell ... You can't. One context = one database.

– Gert Arnold
Nov 20 '18 at 21:25





How can I tell ... You can't. One context = one database.

– Gert Arnold
Nov 20 '18 at 21:25













Please read other answers and comments

– starmucks
Nov 21 '18 at 10:36





Please read other answers and comments

– starmucks
Nov 21 '18 at 10:36












2 Answers
2






active

oldest

votes


















1














If the Company property is from another database, you may need to add a second database context for it, if you haven't already. Something like this:



public class CompanyModelContext : DbContext
{
public CompanyModelContext(DbContextOptions options) : base(options){ }
public DbSet<Company> Company { get; set; }
}





share|improve this answer
























  • The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

    – starmucks
    Nov 20 '18 at 6:03











  • Can you use separate contexts? One that contains a DbSet for the Device table and another context that contains DbSets for User and Company?

    – Mike Black
    Nov 21 '18 at 0:15













  • Yes I can. But since Company refers to User and Device, the Company table will be created in the first database and in the second one. This is the question: how to tell EF table Company is needed only in one database?

    – starmucks
    Nov 21 '18 at 10:36



















0














You can try to set your EF model like this:



Companies table:



[Table("Companies")]
public class Company
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

public ICollection<Device> Devices { get; set; }
}


Devices table:



[Table("Devices")]
public class Device
{
[Key]
public int Id { get; set; }

public Company Company { get; set; }

public int CompanyID { get; set; }
}


ApplicationDbContext class:



public DbSet<Company> Companies { get; set; }

public DbSet<Device> Devices { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Company>(buildAction =>
{
buildAction.ToTable("Companies");

buildAction.HasMany(x => x.Devices)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);
};
}




I've added Devices as a list in Company class, then overriding the FK in the method OnModelCreating.



Once you delete an item in table Company, all items with the same Id in table Devices (comparing to CompanyID) will be delted automatically.



Since we use the 2 properties [Table] and [Key] to define table name and the primary key. So in the OnModelCreating, you can ignore to define the name, PK, FK of table Device.






Company has many User




In this case:



User class:



public class User : IdentityUser
{
public int CompanyId { get; set; }

public Company Company { get; set; }
}


Company class:



[Table("Companies")]
public class Company
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

public ICollection<Device> Devices { get; set; }

public ICollection<User> Users { get; set; }
}


ApplicationDbContext class:



protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Company>(buildAction =>
{
buildAction.ToTable("Companies");

buildAction.HasMany(x => x.Devices)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);

buildAction.HasMany(x => x.Users)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);
};
}





share|improve this answer


























  • The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

    – starmucks
    Nov 20 '18 at 4:01











  • @starmucks Company table cannot have relationshop one-to-one with User table since it has relationship many-to-one with Device table. You can remove it.

    – Foo
    Nov 20 '18 at 4:17











  • Not one-to-one. one-to-many. Company has many User

    – starmucks
    Nov 20 '18 at 4:49











  • @starmucks I've updated the entities.

    – Foo
    Nov 20 '18 at 5:01











  • Well now. Table Company in one database and table Device in another.

    – starmucks
    Nov 20 '18 at 6:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385816%2flink-models-from-different-databases%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If the Company property is from another database, you may need to add a second database context for it, if you haven't already. Something like this:



public class CompanyModelContext : DbContext
{
public CompanyModelContext(DbContextOptions options) : base(options){ }
public DbSet<Company> Company { get; set; }
}





share|improve this answer
























  • The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

    – starmucks
    Nov 20 '18 at 6:03











  • Can you use separate contexts? One that contains a DbSet for the Device table and another context that contains DbSets for User and Company?

    – Mike Black
    Nov 21 '18 at 0:15













  • Yes I can. But since Company refers to User and Device, the Company table will be created in the first database and in the second one. This is the question: how to tell EF table Company is needed only in one database?

    – starmucks
    Nov 21 '18 at 10:36
















1














If the Company property is from another database, you may need to add a second database context for it, if you haven't already. Something like this:



public class CompanyModelContext : DbContext
{
public CompanyModelContext(DbContextOptions options) : base(options){ }
public DbSet<Company> Company { get; set; }
}





share|improve this answer
























  • The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

    – starmucks
    Nov 20 '18 at 6:03











  • Can you use separate contexts? One that contains a DbSet for the Device table and another context that contains DbSets for User and Company?

    – Mike Black
    Nov 21 '18 at 0:15













  • Yes I can. But since Company refers to User and Device, the Company table will be created in the first database and in the second one. This is the question: how to tell EF table Company is needed only in one database?

    – starmucks
    Nov 21 '18 at 10:36














1












1








1







If the Company property is from another database, you may need to add a second database context for it, if you haven't already. Something like this:



public class CompanyModelContext : DbContext
{
public CompanyModelContext(DbContextOptions options) : base(options){ }
public DbSet<Company> Company { get; set; }
}





share|improve this answer













If the Company property is from another database, you may need to add a second database context for it, if you haven't already. Something like this:



public class CompanyModelContext : DbContext
{
public CompanyModelContext(DbContextOptions options) : base(options){ }
public DbSet<Company> Company { get; set; }
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 3:47









Mike BlackMike Black

1112




1112













  • The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

    – starmucks
    Nov 20 '18 at 6:03











  • Can you use separate contexts? One that contains a DbSet for the Device table and another context that contains DbSets for User and Company?

    – Mike Black
    Nov 21 '18 at 0:15













  • Yes I can. But since Company refers to User and Device, the Company table will be created in the first database and in the second one. This is the question: how to tell EF table Company is needed only in one database?

    – starmucks
    Nov 21 '18 at 10:36



















  • The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

    – starmucks
    Nov 20 '18 at 6:03











  • Can you use separate contexts? One that contains a DbSet for the Device table and another context that contains DbSets for User and Company?

    – Mike Black
    Nov 21 '18 at 0:15













  • Yes I can. But since Company refers to User and Device, the Company table will be created in the first database and in the second one. This is the question: how to tell EF table Company is needed only in one database?

    – starmucks
    Nov 21 '18 at 10:36

















The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

– starmucks
Nov 20 '18 at 6:03





The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

– starmucks
Nov 20 '18 at 6:03













Can you use separate contexts? One that contains a DbSet for the Device table and another context that contains DbSets for User and Company?

– Mike Black
Nov 21 '18 at 0:15







Can you use separate contexts? One that contains a DbSet for the Device table and another context that contains DbSets for User and Company?

– Mike Black
Nov 21 '18 at 0:15















Yes I can. But since Company refers to User and Device, the Company table will be created in the first database and in the second one. This is the question: how to tell EF table Company is needed only in one database?

– starmucks
Nov 21 '18 at 10:36





Yes I can. But since Company refers to User and Device, the Company table will be created in the first database and in the second one. This is the question: how to tell EF table Company is needed only in one database?

– starmucks
Nov 21 '18 at 10:36













0














You can try to set your EF model like this:



Companies table:



[Table("Companies")]
public class Company
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

public ICollection<Device> Devices { get; set; }
}


Devices table:



[Table("Devices")]
public class Device
{
[Key]
public int Id { get; set; }

public Company Company { get; set; }

public int CompanyID { get; set; }
}


ApplicationDbContext class:



public DbSet<Company> Companies { get; set; }

public DbSet<Device> Devices { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Company>(buildAction =>
{
buildAction.ToTable("Companies");

buildAction.HasMany(x => x.Devices)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);
};
}




I've added Devices as a list in Company class, then overriding the FK in the method OnModelCreating.



Once you delete an item in table Company, all items with the same Id in table Devices (comparing to CompanyID) will be delted automatically.



Since we use the 2 properties [Table] and [Key] to define table name and the primary key. So in the OnModelCreating, you can ignore to define the name, PK, FK of table Device.






Company has many User




In this case:



User class:



public class User : IdentityUser
{
public int CompanyId { get; set; }

public Company Company { get; set; }
}


Company class:



[Table("Companies")]
public class Company
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

public ICollection<Device> Devices { get; set; }

public ICollection<User> Users { get; set; }
}


ApplicationDbContext class:



protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Company>(buildAction =>
{
buildAction.ToTable("Companies");

buildAction.HasMany(x => x.Devices)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);

buildAction.HasMany(x => x.Users)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);
};
}





share|improve this answer


























  • The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

    – starmucks
    Nov 20 '18 at 4:01











  • @starmucks Company table cannot have relationshop one-to-one with User table since it has relationship many-to-one with Device table. You can remove it.

    – Foo
    Nov 20 '18 at 4:17











  • Not one-to-one. one-to-many. Company has many User

    – starmucks
    Nov 20 '18 at 4:49











  • @starmucks I've updated the entities.

    – Foo
    Nov 20 '18 at 5:01











  • Well now. Table Company in one database and table Device in another.

    – starmucks
    Nov 20 '18 at 6:02
















0














You can try to set your EF model like this:



Companies table:



[Table("Companies")]
public class Company
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

public ICollection<Device> Devices { get; set; }
}


Devices table:



[Table("Devices")]
public class Device
{
[Key]
public int Id { get; set; }

public Company Company { get; set; }

public int CompanyID { get; set; }
}


ApplicationDbContext class:



public DbSet<Company> Companies { get; set; }

public DbSet<Device> Devices { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Company>(buildAction =>
{
buildAction.ToTable("Companies");

buildAction.HasMany(x => x.Devices)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);
};
}




I've added Devices as a list in Company class, then overriding the FK in the method OnModelCreating.



Once you delete an item in table Company, all items with the same Id in table Devices (comparing to CompanyID) will be delted automatically.



Since we use the 2 properties [Table] and [Key] to define table name and the primary key. So in the OnModelCreating, you can ignore to define the name, PK, FK of table Device.






Company has many User




In this case:



User class:



public class User : IdentityUser
{
public int CompanyId { get; set; }

public Company Company { get; set; }
}


Company class:



[Table("Companies")]
public class Company
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

public ICollection<Device> Devices { get; set; }

public ICollection<User> Users { get; set; }
}


ApplicationDbContext class:



protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Company>(buildAction =>
{
buildAction.ToTable("Companies");

buildAction.HasMany(x => x.Devices)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);

buildAction.HasMany(x => x.Users)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);
};
}





share|improve this answer


























  • The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

    – starmucks
    Nov 20 '18 at 4:01











  • @starmucks Company table cannot have relationshop one-to-one with User table since it has relationship many-to-one with Device table. You can remove it.

    – Foo
    Nov 20 '18 at 4:17











  • Not one-to-one. one-to-many. Company has many User

    – starmucks
    Nov 20 '18 at 4:49











  • @starmucks I've updated the entities.

    – Foo
    Nov 20 '18 at 5:01











  • Well now. Table Company in one database and table Device in another.

    – starmucks
    Nov 20 '18 at 6:02














0












0








0







You can try to set your EF model like this:



Companies table:



[Table("Companies")]
public class Company
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

public ICollection<Device> Devices { get; set; }
}


Devices table:



[Table("Devices")]
public class Device
{
[Key]
public int Id { get; set; }

public Company Company { get; set; }

public int CompanyID { get; set; }
}


ApplicationDbContext class:



public DbSet<Company> Companies { get; set; }

public DbSet<Device> Devices { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Company>(buildAction =>
{
buildAction.ToTable("Companies");

buildAction.HasMany(x => x.Devices)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);
};
}




I've added Devices as a list in Company class, then overriding the FK in the method OnModelCreating.



Once you delete an item in table Company, all items with the same Id in table Devices (comparing to CompanyID) will be delted automatically.



Since we use the 2 properties [Table] and [Key] to define table name and the primary key. So in the OnModelCreating, you can ignore to define the name, PK, FK of table Device.






Company has many User




In this case:



User class:



public class User : IdentityUser
{
public int CompanyId { get; set; }

public Company Company { get; set; }
}


Company class:



[Table("Companies")]
public class Company
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

public ICollection<Device> Devices { get; set; }

public ICollection<User> Users { get; set; }
}


ApplicationDbContext class:



protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Company>(buildAction =>
{
buildAction.ToTable("Companies");

buildAction.HasMany(x => x.Devices)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);

buildAction.HasMany(x => x.Users)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);
};
}





share|improve this answer















You can try to set your EF model like this:



Companies table:



[Table("Companies")]
public class Company
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

public ICollection<Device> Devices { get; set; }
}


Devices table:



[Table("Devices")]
public class Device
{
[Key]
public int Id { get; set; }

public Company Company { get; set; }

public int CompanyID { get; set; }
}


ApplicationDbContext class:



public DbSet<Company> Companies { get; set; }

public DbSet<Device> Devices { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Company>(buildAction =>
{
buildAction.ToTable("Companies");

buildAction.HasMany(x => x.Devices)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);
};
}




I've added Devices as a list in Company class, then overriding the FK in the method OnModelCreating.



Once you delete an item in table Company, all items with the same Id in table Devices (comparing to CompanyID) will be delted automatically.



Since we use the 2 properties [Table] and [Key] to define table name and the primary key. So in the OnModelCreating, you can ignore to define the name, PK, FK of table Device.






Company has many User




In this case:



User class:



public class User : IdentityUser
{
public int CompanyId { get; set; }

public Company Company { get; set; }
}


Company class:



[Table("Companies")]
public class Company
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

public ICollection<Device> Devices { get; set; }

public ICollection<User> Users { get; set; }
}


ApplicationDbContext class:



protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Company>(buildAction =>
{
buildAction.ToTable("Companies");

buildAction.HasMany(x => x.Devices)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);

buildAction.HasMany(x => x.Users)
.WithOne(x => x.Company)
.OnDelete(DeleteBehavior.Cascade);
};
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 5:01

























answered Nov 20 '18 at 3:47









FooFoo

1




1













  • The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

    – starmucks
    Nov 20 '18 at 4:01











  • @starmucks Company table cannot have relationshop one-to-one with User table since it has relationship many-to-one with Device table. You can remove it.

    – Foo
    Nov 20 '18 at 4:17











  • Not one-to-one. one-to-many. Company has many User

    – starmucks
    Nov 20 '18 at 4:49











  • @starmucks I've updated the entities.

    – Foo
    Nov 20 '18 at 5:01











  • Well now. Table Company in one database and table Device in another.

    – starmucks
    Nov 20 '18 at 6:02



















  • The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

    – starmucks
    Nov 20 '18 at 4:01











  • @starmucks Company table cannot have relationshop one-to-one with User table since it has relationship many-to-one with Device table. You can remove it.

    – Foo
    Nov 20 '18 at 4:17











  • Not one-to-one. one-to-many. Company has many User

    – starmucks
    Nov 20 '18 at 4:49











  • @starmucks I've updated the entities.

    – Foo
    Nov 20 '18 at 5:01











  • Well now. Table Company in one database and table Device in another.

    – starmucks
    Nov 20 '18 at 6:02

















The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

– starmucks
Nov 20 '18 at 4:01





The problem is that the Device table (the first database) and the User table (the second database) refer to the Company table (the second database) and so EF creates the Company table in both databases.

– starmucks
Nov 20 '18 at 4:01













@starmucks Company table cannot have relationshop one-to-one with User table since it has relationship many-to-one with Device table. You can remove it.

– Foo
Nov 20 '18 at 4:17





@starmucks Company table cannot have relationshop one-to-one with User table since it has relationship many-to-one with Device table. You can remove it.

– Foo
Nov 20 '18 at 4:17













Not one-to-one. one-to-many. Company has many User

– starmucks
Nov 20 '18 at 4:49





Not one-to-one. one-to-many. Company has many User

– starmucks
Nov 20 '18 at 4:49













@starmucks I've updated the entities.

– Foo
Nov 20 '18 at 5:01





@starmucks I've updated the entities.

– Foo
Nov 20 '18 at 5:01













Well now. Table Company in one database and table Device in another.

– starmucks
Nov 20 '18 at 6:02





Well now. Table Company in one database and table Device in another.

– starmucks
Nov 20 '18 at 6:02


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385816%2flink-models-from-different-databases%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini