0

I have a question regarding EF Code First with MVC. Currently I know how to create controllers and views out a model that has all the atributtes as native types such as (int, string, etc.).

What if I have a model that one its attributes is a type defined in another model in my project?

Something like

public class Partido
    {
        public int ID { get; set; }
        public Seleccion Local { get; set; }
        public Seleccion Visitante { get; set; }
        public Resultado Marcador { get; set; }
    }

Where Seleccion and Resultado are defined each one in the same model namespace of the project.

3
  • Right click on Controller folder, tell I want a new controller out of Partido model and one context. Then I repeat the same but with the View folder. Commented Jun 8, 2012 at 12:57
  • And what was the result? Commented Jun 8, 2012 at 13:00
  • I see nothing. The generated HTML is empty. I asume that is because it does not recognize Seleccion and Resultado plus ID is the PK for the table in the DB. Commented Jun 8, 2012 at 13:02

1 Answer 1

1

Decorate Seleccion and Resultado classes with ComplexType attribute. Example:

public class Partido
{
    public int ID { get; set; }
    public Seleccion Local { get; set; }
    public Seleccion Visitante { get; set; }
    public Resultado Marcador { get; set; }
}

[ComplexType]
public class Seleccion {} 

[ComplexType]
public class Resultado {}

Entity Framework will generate appropriate colums in 'Partido' table that will correspond to Seleccion and Resultado class properties.

You can learn more about it here: Code First Data Annotations

Sign up to request clarification or add additional context in comments.

5 Comments

It didn't work I think becasue reading the link you provided the ComplexType they cannot be tracked in the database using and ID which I do in the Seleccion and Resultado definition.
Yes, these classes must act as value objects and cannot have key properties.
So, basically it is imposible to generate automatically a controller/view out of model that is compound by another model that does have a key? So, I guess it is becasue you shouldn't do that. My question now is, what should you do in that case? Which pattern do I ought to follow? Thanks!!
You could also try to decorate Selection and Resultado id properties with [NotMapped]. I'm not sure if it's going to work, because I've never tried it. Btw why can't you treat these classes as entities and put them in your DbContext class? EF will create tables for them and in Partido entity they will be treated as foreign keys.
The idea is that ''Seleccion'' and ''Resultado'' they are entities as well. I mean, the same way ''Partido'' is. I am not sure if I understood you correctly in that last point...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.