Entity model convert to C# Model type

Posts   
 
    
Posts: 8
Joined: 19-Mar-2014
# Posted on: 22-Mar-2014 14:11:45   

Hi. I have a system and I need to convert type Entity model to my User Model in mvc 4. I will explain simple_smile

I have a register method like this and this method return GlobalUser type.

            GlobalUserEntity user = new GlobalUserEntity();
            user.IsNew = true;
            user.UserName = _user.UserName;
            user.Password = _user.Password;
            user.Name = _user.Name;
            user.Surname = _user.Surname;
            user.Email = _user.EMail;
            user.Date = DateTime.Now;
            user.ActivationCode = "123";
            user.Active = false;
            user.Save();
            GlobalUser newUser = new GlobalUser();

            newUser ===== user ?????

            return newUser;

How can I equal Entity type to Model type. This give a error Cannot implicitly type Entity to Model.

And also I dont wanna do this way.

            newUser.GlobalID = _user.GlobalID;
            newUser.Name = _user.Name;
            newUser.Password = _user.Password;
            .
            .
            .
            .

Cause its not a good way I think.

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Mar-2014 09:23:07   

Why do you have a GlobalUser class? You can use your generated GlobalUserEntity as your MVC model, you don't have to have both classes.

If you really need GlobalUser class, you have to write that code that you don't want, or use a tool like Automapper.

David Elizondo | LLBLGen Support Team
Posts: 8
Joined: 19-Mar-2014
# Posted on: 13-May-2014 12:50:42   

Yes I used Automapper and I handled it ! It solved thanks

Posts: 8
Joined: 19-Mar-2014
# Posted on: 29-May-2014 20:34:59   

Hi,

As you said before, how can I generated GlobalUserEntity as your MVC model in LLBL ? I need to do this right now simple_smile

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 30-May-2014 07:32:24   

fatihayyildiz wrote:

As you said before, how can I generated GlobalUserEntity as your MVC model in LLBL ? I need to do this right now simple_smile

To do it in one way (show data in your view) you just pass your entity/collection to the ViewBag.Model or ViewState.Model and create your view to expect that type. To do it back-and-forth you have to use a custom binder to translate controls to actual entities. Brian Chance wrote a LLBLGenModelBinder.

At the following thread there is an example, using MCV and LLBLGenModelBinder: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=19731&StartAtMessage=0&#111280

HTH wink

David Elizondo | LLBLGen Support Team