CRUD with Artificial Types

Thread is closed for posting
3 posts, 0 answers
  1. Mike
    Mike avatar
    7 posts
    Member since:
    Sep 2009

    Posted 31 Aug 2011 Link to this post

    I am looking to use artificial types/properties in my app and am trying to put all the scattered pieces together on how to handle some real world scenarios.
    I've been able to do the following:
    1. Extend my existing model following the guide in the documentation section "Extend Existing Models with New Types"
    2. Create new artificial types and run the schema update against the database
    3. Create new instances of the artificial types to insert new entries like so:


    IPersistentTypeDescriptor customdataDescriptor = CustomDataContext.Scope.PersistentMetaData.GetPersistentTypeDescriptor("customfieldsNamespace.customfields");
     object customdata = customdataDescriptor.CreateInstance(1);


    4. Also figured out how to query using dynamic linq like:

    var test = CustomDataContext.Scope.ExtentByName("customfieldsNamespace.customfields")
                    .Where("testcolumn == @0", "test");


    Is there a beter way to query artificial types?

    Now I'm having trouble with trying update and delete. Are there any examples that show how to do all CRUD operations using artificial types? I'm not sure where to begin. I understand that I can set values using:


    PropertyDescriptor productNameDescriptor = productDescriptor.GetProperties()["name"];
     productNameDescriptor.SetValue(product, "My Product");

    but how can I load an existing record from the database into the PropertyDescriptor instance to be updated?
    Also... how can I do a delete operation on an Artificial Type instance?

    Any guidance is appreciated.


  2. Nikola
    Nikola avatar
    27 posts

    Posted 06 Sep 2011 Link to this post

    Hi Mike,

    Sorry for the late response. You have actually found the best way to query the context for artificial types.
    Another approach would be to dynamically create a generic implementation of the scope Extent<> method that would utilize your artificial type. Something like this:

    Type FindArtificial(string name)
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            if (assembly.IsDynamic)
                Type t = assembly.GetType(name, false);
                if (t != null)
                    return t;
        throw new ArgumentOutOfRangeException("name", name, "Type not found");
    public void QueryArtificialType()
        Type t = FindArtificial("Telerik.OpenAccess.Tests.CSModel.BType");
        MethodInfo mi =typeof(ExtensionMethods).GetMethod("Telerik.OpenAccess.Extent").MakeGenericMethod(t);
        IQueryable e = (IQueryable)mi.Invoke(null, new object[] { Scope });

    As for you other questions, here is an example of how you can obtain and modify an artificial object of type Car. Getting a type descriptor for a loaded instance can be done with the GetExtendedTypeDescriptor

    IQueryable<PersistenceCapable> query
        = fluentContext.Scope.ExtentByName("ConsoleApplication10.Car").Where("CarID == @0", carId) as IQueryable<PersistenceCapable>;
    PersistenceCapable instance = query.FirstOrDefault();
    ICustomTypeDescriptor descriptor = fluentContext.Scope.PersistentMetaData.GetExtendedTypeDescriptor(instance);
    PropertyDescriptor propertyDescriptor = descriptor.GetProperties()["make"];
    propertyDescriptor.SetValue(instance, "Opel");


    Alternatively, you could invoke the following code if you wanted to delete this Car.

    If you need any further assistance please don't hesitate to ask.

     All the best,
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

  3. Mike
    Mike avatar
    7 posts
    Member since:
    Sep 2009

    Posted 07 Sep 2011 Link to this post

    Thank you! This helped tremendously.

Back to Top