Entity Framework Code First. How to generate two banks from two projects, with the second project using classes from the first

Asked

Viewed 274 times

0

Personal talk!

I have a question regarding Entity Framework 6.

I have two projects A and B, A being a shared core. It has some classes as Parents, State, City, Address etc.

In Project B, I have some classes like Candidate, Vacancy etc.

In this case, project B classes refer to project A classes, such as:

public class Candidato
{
    public virtual int IdEndereco { get; set; };
    public virtual Endereco Endereco { get; set; }
}

I have two contexts, one for each project. Each project will have its own database.

When starting the project, the banks are created normally for the two projects.

The problem is that for all A classes referenced by B, a table is created in the project B database.

However, these tables should only exist in the project A database.

The bank was like this:

A (Country, State, City, Address)

B (Country, State, City, Address, Candidate, Vacancy)

Are there any settings in Entity to say that A classes referenced by B should not have tables created in the project B database?

Thank you.

  • Of course he will create the tables, as you want to be created a relationship between two tables and one of them doesn’t even exist?

  • The table will exist, but in another bank. In case, would you have to change the class to not have a strong relationship? Type: class Candidate { public int Idendereco { get; set; } } No virtual, no Address variable

  • 2

    With the information you presented is what I think should be done, relate the Ids without creating real relationships in the database. What is the need to keep these seats separate? If it is to be a shared bank for several applications, I think it would be better to think of a more elegant solution...

1 answer

0


If you use [Notmapped] Migration ignores the relation, but the address property will always be null, you will have to load otherwise.

public class Candidato
{
    public virtual int IdEndereco { get; set; };
    [NotMapped]
    public virtual Endereco Endereco { get; set; }
}

Thus this property Address would not be filled in the query because this relationship does not exist in the current context.

You can also use with Fluent (it is more advisable not to pollute your entity):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(modelBuilder.Entity<Candidato>               ().Ignore(x => x.Endereco));
        base.OnModelCreating(modelBuilder);
    }

I would use GUID instead of int for the ID since the relationships are between different banks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.