none
how to use reverse engineering in entity framework at runtime RRS feed

  • Question

  • as can rebuild my classes with reverse engineering and entity framework for my existing database at runtime ie if I create a new table when I'm running my project to generate the classes again but stop the project being always on time of execution
    Friday, May 9, 2014 7:15 PM

Answers

  • Hi Alexis;

    01 var a = db.Database.ExecuteSqlCommand("alter table TB_Login ADD " + NombreCampo + " " + TipoDato);
    02 string vari1 = NombreCampo;
    03 string vari2 = TipoDato;
    04 db.SaveChanges();

    A couple of things with these lines of code above. Line number 2 and 3 should come before line 1. Because you are setting values in the SQL command that is to be sent to the SQL server. Line 4 in this case is not needed. The ExecuteSqlCommand is not processed by the DbContext but bypasses it and sent directly to the SQL server to be processed. At this point you have changed the database table but the model in your program has not changed to accommodate the changes you made to the database and therefore you can not access that new column until you terminate the program update the classes in the model and re-run the program.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.



    Tuesday, May 13, 2014 3:25 AM

All replies

  • Hi Alexis;

    If you are talking about reverse engineering in the Entity Framework Power Tools, this is not a run time tool. It was developed to get information from an existing database, as stated in the web site "Reverse Engineer Code First - Generates POCO classes, derived DbContext and Code First mapping for an existing database." it also states that it is a "design-time features for DbContext". So it does not have any features to do this at run time.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Saturday, May 10, 2014 12:52 PM
  • Hello
    Well there a way to update the model of entity framework at runtime I explained I create a new column at runtime and when the grid step brings me an error that the new column does not exist in some form of model abria update at runtime

    thanks in advance
    Sunday, May 11, 2014 3:46 AM
  • To your statement: 

    Well there a way to update the model of entity framework at runtime I explained I create a new column at runtime and when the grid step brings me an error that the new column does not exist in some form of model abria update at runtime 

    Well what I think you did was to create a new column in the database and NOT the model. The model is created on the local system when you execute your first Entity Framework command and uses the POCO classes to create it. This model maps the database to the classes in your program. Because the classes in your program does not know anything about this new column you can not access it and the reason why you get the error, "new column does not exist".

    Can you post the code which you state you add a new column to the model at run time and also how you attempt to get the data for the new column.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Sunday, May 11, 2014 2:46 PM
  •  that is the function to create the column

    [HttpPost]
            public ActionResult Agregarcampo(string NombreTabla, string NombreCampo, string TipoDato, string tb1)
            {



                    var a = db.Database.ExecuteSqlCommand("alter table TB_Login ADD " + NombreCampo + " " + TipoDato);
                    string vari1 = NombreCampo;
                    string vari2 = TipoDato;
                    db.SaveChanges();



                return View("GridViewUsuario");
            }

    and this is the code as I bring the gridview

    @{
    var grid = Html.DevExpress().GridView(settings => {
            settings.Name = "GridView";
            settings.CallbackRouteValues = new { Controller = "Inicio", Action = "GridViewPartial" };
            
    settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Inicio", Action = "GridViewPartialAddNew" };
            settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Inicio", Action = "GridViewPartialUpdate" };
            settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Inicio", Action = "GridViewPartialDelete" };
            settings.SettingsEditing.Mode = GridViewEditingMode.EditFormAndDisplayRow;
            settings.SettingsBehavior.ConfirmDelete = true;

    settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowNewButton = true;
            settings.CommandColumn.ShowDeleteButton = true;
            settings.CommandColumn.ShowEditButton = true;
            settings.ControlStyle.CssClass = "GridView";

            settings.KeyFieldName = "Usuario";

    settings.SettingsPager.Visible = true;
    settings.Settings.ShowGroupPanel = true;
    settings.Settings.ShowFilterRow = true;
            settings.SettingsBehavior.AllowSelectByRowClick = true;

            B3.Models.B3EntityEntities e = new B3.Models.B3EntityEntities();

            foreach (var item in ViewBag.a)
            {
                    settings.Columns.Add(item);
            }
    });
    if (ViewData["EditError"] != null){
            grid.SetEditErrorText((string)ViewData["EditError"]);
        }
    }
    @grid.Bind(Model).GetHtml()                            
    Monday, May 12, 2014 2:51 AM
  • Hi Alexis;

    01 var a = db.Database.ExecuteSqlCommand("alter table TB_Login ADD " + NombreCampo + " " + TipoDato);
    02 string vari1 = NombreCampo;
    03 string vari2 = TipoDato;
    04 db.SaveChanges();

    A couple of things with these lines of code above. Line number 2 and 3 should come before line 1. Because you are setting values in the SQL command that is to be sent to the SQL server. Line 4 in this case is not needed. The ExecuteSqlCommand is not processed by the DbContext but bypasses it and sent directly to the SQL server to be processed. At this point you have changed the database table but the model in your program has not changed to accommodate the changes you made to the database and therefore you can not access that new column until you terminate the program update the classes in the model and re-run the program.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.



    Tuesday, May 13, 2014 3:25 AM