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

0

Speak up!

I have a question regarding Entity Framework 6.

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

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

In this case, classes in project B refer to classes in project A, 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 you start the project, banks are typically created for both projects.

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

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

The bank looks like this:

A (Country, State, City, Address)

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

Is there a setting in the Entity to say that the classes of A referenced by B should not have tables created in the project bank B?

Thank you.

    
asked by anonymous 04.07.2016 / 19:00

1 answer

0

If you use [NotMapped], the migration ignores the relationship, however the address property will always be null, you will have to load it in another way.

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

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

You can also use with fluent (it is most 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.

    
06.07.2016 / 17:05