How check by unit test that properties mark as computed in ORM model?

后端 未结 1 1647
说谎
说谎 2020-12-12 07:32

I\'m created ORM by Entity Framework 5.0 (C# 4.5) - database first.

Some properties of entities i\'m marked as computed (binded to columns with defaults).

Ho

相关标签:
1条回答
  • 2020-12-12 08:22

    I'm not sure if this applies to your case - but if you want to read the metadata at runtime - from the EntityFramework model you could try a few things mentioned in my earlier post here (and further improved by the OP)...

    How I can read EF DbContext metadata programmatically?

    That talks about DbContext (which you can work with from any side, so that also applies to you) - but specifically, just get the ObjectContext - and continue from this point...

    var container = objectContext.MetadataWorkspace.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace);
    
    // and just to get you started... 
    var baseset = objectContext
        .MetadataWorkspace
        .GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace)
        .BaseEntitySets
        .First(meta => meta.ElementType.Name == "MyBaseClass");
    
    var elementType = objectContext
        .MetadataWorkspace
        .GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace)
        .BaseEntitySets
        .First(meta => meta.ElementType.Name == "MyBaseClass")
        .ElementType;
    
    EdmMember member = elementType.Members[2]; // e.g. 3rd property
    Facet item;
    if (member.TypeUsage.Facets.TryGetValue("StoreGeneratedPattern", false, out item))
    {
        var value = ((StoreGeneratedPattern)item.Value) == StoreGeneratedPattern.Computed;
    }
    

    You get the MetadataWorkspace and you can work your way down from there.

    We managed to extract navigation properties etc. - but there might be some other info for each property - like calculated. I haven't tried but it might help.

    Also I haven't tried this on the model or database first - but I don't see why it shouldn't work - the infrastructure is the same (EF, not code first).

    EDIT: I added a more specific code to get you started (see edited code). That kind of works (gets you where the 'facets' are stored), it isn't ready-to-use code, more work is needed.

    0 讨论(0)
提交回复
热议问题