locked
Instantiate database model entities RRS feed

  • Question

  • User1043796333 posted

    Split off from http://forums.asp.net/t/1732043.aspx/1?Instantiate+database+model+entities

    Naughton's answer in a recent post comes like this:

            protected void Page_Init(object sender, EventArgs e)
    {
    table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
    GridView1.SetMetaTable(table, table.GetColumnValuesFromRoute(Context));
    GridDataSource.EntityTypeFilter = table.EntityType.Name;
    if(table.Name.Equals("Employee")
    GridDataSource.Where = "it.DEPT>3000";
    }

    If you see here in the code above,

    table = DynamicDataRouteHandler.GetRequestMetaTable(Context);

    "table" is a variable of the type DynamicDataRouteHandler.GetRequestMetaTable(Context) NOT A STRING

    But Dynamic Data maintains its properties just when it is executing that piece of code, just in that very moment. After that: "table" ... IT'S GONE

    There is no way of recreating that sentence anywhere else but in that method which has all it needs to work properly

    I'd like to do the same (using an instance of that string) in any other method/event of my ListDtails.aspx page

    Thanks

    Saturday, February 4, 2012 12:07 PM

Answers

  • User-330204900 posted

    Sorry Topolov, this

    protected void DetailsDataSource_Inserting(object sender, EntityDataSourceChangingEventArgs e)
    {
        var thisentity = table.Name;
        thisentity theentity1 = (thisentity)(e.Entity);
        DataSourceOnInserting(theentity);
        GridView1.DataBind();
    }

    this code is nonsence as the variable thisentity will be of type string as that is the type of table.Name? so the case here (thisentity)(e.Entity) make no sense?

    protected void DetailsDataSource_Inserting(object sender, EntityDataSourceChangingEventArgs e)
    {
        var theentity = (Models.Restriction)(e.Entity);
        DataSourceOnInserting(theentity);
        GridView1.DataBind();
    }
    

     this is what you are trying to achive but there is no way to cast the entity without knowing the type before hand.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 12, 2012 10:39 AM

All replies

  • User-330204900 posted

    "table" is a variable of the type DynamicDataRouteHandler.GetRequestMetaTable(Context) NOT A STRING

    Actually it's of the type MetaTable.

    There is no way of recreating that sentence anywhere else but in that method which has all it needs to work properly

    Which sentence, this?

    if(table.Name.Equals("Employee"))

    This can be put anywhere after is has been set in the Page Init as it's a field on the page.

    I'd like to do the same (using an instance of that string) in any other method/event of my ListDtails.aspx page

    Still not sure what you are asking, the table is just metadata about the entity i.e. it contains the attribute and the column info, what do you want to use that for?

     

     

    Thursday, February 9, 2012 5:28 AM
  • User1043796333 posted

    I have to tell you something Stephen Naughton:you must read this question with an IQ in the order of 80 to a 100 max

    This is not a question for intelectuals. I am pretty serious on this. If you don't lower your mind to a level of normal perople level; reasoning then this question will remain unsolved until next framework 5.o or later, in which case it will be worthless having asked anyway

    Please, have in mind what I am saying and if you are not willing to go to that lower level then please say it. This is not a joke but a remainders that maybe this question is so silly that you wont even understand because you thinks this might be something compicated

    This is a trivial, simple, non-elaborated question. Have that in mind 

    I have written something like this that I want to avoid: wusing a switch statement 

            protected void DetailsDataSource_Inserting(object sender, EntityDataSourceChangingEventArgs e)
            {
                var thisentity = table.Name;
                switch (thisentity)
                {
                    case "a01acc_accountdets":
                        a01acc_accountdets theentity1 = (a01acc_accountdets)(e.Entity);
                        DataSourceOnInserting(theentity1);
                        break;
                    case "m01mst_finacctypes":
                        m01mst_finacctypes theentity2 = (m01mst_finacctypes)(e.Entity);
                        DataSourceOnInserting(theentity2);
                        break;
                    case "m03mst_attributetypes":
                        m03mst_attributetypes theentity4 = (m03mst_attributetypes)(e.Entity);
                        DataSourceOnInserting(theentity4);
                        break;
                    case "m04mst_bonificationtypes":
                        m04mst_bonificationtypes theentity5 = (m04mst_bonificationtypes)(e.Entity);
                        DataSourceOnInserting(theentity5);
                        break;
    
                        ...
                        ...
                        ...
                        ...
    
                    case "t82tbl_typeofunits":
                        t82tbl_typeofunits theentity82 = (t82tbl_typeofunits)(e.Entity);
                        DataSourceOnInserting(theentity82);
                        break;
                    case "t83tbl_quotations":
                        t83tbl_quotations theentity83 = (t83tbl_quotations)(e.Entity);
                        DataSourceOnInserting(theentity83);
                        break;
                }
                GridView1.DataBind();
            }
    

    I could have all of this in a simple sentence (if the variable of the type var could be instantiated)

            protected void DetailsDataSource_Inserting(object sender, EntityDataSourceChangingEventArgs e)
            {
                var thisentity = table.Name;
                thisentity theentity1 = (thisentity)(e.Entity);
                DataSourceOnInserting(theentity);           
                GridView1.DataBind();
            }
    

    Saturday, February 11, 2012 11:38 PM
  • User-330204900 posted

    Sorry Topolov, this

    protected void DetailsDataSource_Inserting(object sender, EntityDataSourceChangingEventArgs e)
    {
        var thisentity = table.Name;
        thisentity theentity1 = (thisentity)(e.Entity);
        DataSourceOnInserting(theentity);
        GridView1.DataBind();
    }

    this code is nonsence as the variable thisentity will be of type string as that is the type of table.Name? so the case here (thisentity)(e.Entity) make no sense?

    protected void DetailsDataSource_Inserting(object sender, EntityDataSourceChangingEventArgs e)
    {
        var theentity = (Models.Restriction)(e.Entity);
        DataSourceOnInserting(theentity);
        GridView1.DataBind();
    }
    

     this is what you are trying to achive but there is no way to cast the entity without knowing the type before hand.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 12, 2012 10:39 AM
  • User1043796333 posted

    Thanks Naughton

    At least I now know that there is no solution to my question

    You see: it was really simply to answer

    I have to admit that my code probably sucks and that's why you didn't understand what I was asking for

    Thanks for coming to the non-priviliged mind world (all of us with IQ below a 100)

    Sunday, February 12, 2012 1:04 PM
  • User1043796333 posted

    P.S. In my case I have to write a switch statement for almost 240 tables

    That sucks too and that's shy I was looking for a simpler way of writing this

    If you have to redefine your database model .... then rerwriting the switch sentence is a crying shame

    Thanks anyway

    Sunday, February 12, 2012 1:06 PM
  • User-330204900 posted

    There is a solution do this in you data model, there are plenty of post on doing this osrt of thing in the data model see:

    these are just a few that I could find

     

    Sunday, February 12, 2012 1:44 PM
  • User1043796333 posted

    This is by no means a rebutal to some of the answers provided in your links

    BUT I must admit that I have some doubts

    If ASP.Net Dynamic Data is intended to work based on templates then it is fair enough to think that there shouldn't be any need of doing any customization to your code but rather working with code that should be VALID for ANY INSTANCE OF ANY ENTITY

    For example in http://blogs.msdn.com/b/simonince/archive/2009/04/20/auditing-data-changes-in-the-entity-framework-part-2.aspx you find the following code:

    using (MyEntities db = new MyEntities())
    {
        db.Audit<Product, ProductHistory>(
            (record, action) => new ProductHistory() {
                Id = record.Field<Product, int>(f => f.Id),
                Description = record.Field<Product, string>(
                    f => f.Description),
                Price = record.Field<Product, double>(f => f.Price),
                CreatedDate = record.Field<Product, DateTime>(
                    f => f.CreatedDate),
                CreatedBy = record.Field<Product, string>(
                    f => f.CreatedBy),
                ChangedBy = "Simon",
                ChangedDate = DateTime.Now,
                ChangeType = action.ToString() },
            (ph) => db.AddToProductHistory(ph));
     
        // TODO: make some changes to Product here...
     
        db.SaveChanges();
    }

    I know that that piece of code belongs to "Products" entity only BUT my question goes in this sense:

    Why do you have to write something like:

    Id = record.Field<Product, int>(f => f.Id),
    instead of
    Id = record.Field<AnyEntity, int>(f => f.Id),
    In which AnyEntity is any table from your database model passed to this emthod as a parameter which will have to be evaluated at runtime?
    Why the need of writing customized code?
    That's what I call a crying shame because this kind of code should obbeyto a template designed framework as it is Dynamic Data

    Sunday, February 12, 2012 6:18 PM
  • User1043796333 posted

    P.S.:

    So what's the need of manually instantiating 240 entities as to have an available instantiation of each one of them

    In my original question, firts post of this question, I mentioned that it should be very good and fair not having the programmer doing manual instantiations of the database mdoel entities but rather working with "templated based" code which will be capable of doing dynamic instantiations on the fly

    Everyone should expect that in a templated environment, isn't it?

    Sunday, February 12, 2012 6:23 PM
  • User-330204900 posted

    I never do instatiate entities I always use a "Templateing" approach and as a result I never do any cod ein the templates to do with the entities, at most I may need to create 1 or two custom pages to perform some custom function that is either Entity based and thus no generic or a particular funtions or operation that is only applicable to a single entity and so not worth making generic.

    Sunday, February 12, 2012 7:19 PM
  • User1043796333 posted

    I really don't understand how can you avoid writing  switch sentence inside a data source control from within your database model itself

    Sunday, February 12, 2012 8:06 PM
  • User-330204900 posted

    I will post my sample code for doing simple auditing for DD

    Basic Auditing for Dynamic Data with Entity Framework 4.x

    Hope this helps

     

    Monday, February 13, 2012 7:51 AM
  • User-326746839 posted

    Hi Topolov,

    You´re right. This is an overlooked question and it certainly requires no more than a 80 IQ to solve it.

    Carlos Porras

    (El Salvador)

    Tuesday, February 14, 2012 10:27 AM
  • User1043796333 posted

    So the answer is???

    Tuesday, February 14, 2012 6:58 PM
  • Wednesday, February 15, 2012 1:16 PM
  • User1432255915 posted

    Hi Steve,

    Could you please let me know when you post your sample code for doing simple auditing for DD?

    Cheers

    C

    Monday, February 20, 2012 12:29 PM
  • User-330204900 posted

    Basic Auditing for Dynamic Data with Entity Framework 4.x

    The code is on my blog at the link I have shown above.

    Monday, February 20, 2012 12:36 PM
  • User1432255915 posted

    ok, so you do not have a solution file for this tutorial. It is just the code, right?

     

    Monday, February 20, 2012 2:04 PM
  • User-330204900 posted

    yes it's just the code, I can do a sample if you like though?

    Monday, February 20, 2012 6:04 PM
  • User1432255915 posted

    Hi Steve,

    Yes. If you could do a sample it would be great :-)

    By the way, I cannot get the Builder database to work at all using the script and excel file you provided. By reading some of the comments in your blog I noticed that you emailed the databse to some members.

    Could you pleae email the databse to my email as well?

    Cheers

    C

     

    Tuesday, February 21, 2012 5:37 AM
  • User-330204900 posted

    I'll see if I can find or recreate it :)

    Tuesday, February 21, 2012 5:44 AM
  • User-330204900 posted

    I have the DB in SQL Server 2008 R2 format but nothing else will that do?

    Tuesday, February 21, 2012 5:51 AM
  • User-330204900 posted

    Just sent you the DB as a Backup from SQL 2005

    Tuesday, February 21, 2012 7:46 AM
  • User1432255915 posted

    Yes, thanks very much Steve. I am using SQL 2008.

    Cheer

    C

    Tuesday, February 21, 2012 9:29 AM
  • User1432255915 posted

    Thank you.

    Tuesday, February 21, 2012 9:30 AM