How to programatically generate CSDL or EDMX from a set of existing POCO objects
I'd like to simulate what WCF Data Services does with it's "$metadata" tag... that is, send a CSDL document that describes an existing set of objects that may (or may not) be part of an Entity Framework model. In fact, assume the EF is not part of this discussion...
I want to create a service that inspects the type of objects that it can return, generates a CSDL document and sends that to the client so that the client can code-gen those objects from the CSDL (using EDMGen should work). Once the client has generated the objects (and loaded the generated assembly), it can receive strongly-typed data from the service.
It does not seem like EDMGen can be used to generate CSDL (or EDMX) from POCOs... it can do it from a db-connection and you can generate POCOs from CSDL, but no the other way. Is there a way to this that anyone knows?
Concrete Example
Given this code:
public class DirectoryEntry
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public ICollection<DirectoryEntryProperty> Properties { get; set; }
}
public class DirectoryEntryProperty
{
public int Id { get; set; }
public string Key { get; set; }
public string Value { get; set; }
public DirectoryEntry DirectoryEntry { get ; set; }
}
I'd like to generate this document:
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="DirectoryService" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:ib10="http://www.ideablade.com/edm/2010" ib10:Tag="DirectoryService">
<EntityContainer Name="DirectoryServiceContainer" annotation:LazyLoadingEnabled="true" ib10:GenerateDeveloperClasses="true" ib10:DevForceEnabled="false">
<EntitySet Name="DirectoryEntries" EntityType="DirectoryService.DirectoryEntry" />
<EntitySet Name="DirectoryEntryProperties" EntityType="DirectoryService.DirectoryEntryProperty" />
<AssociationSet Name="DirectoryEntryPropertyDirectoryEntry" Association="DirectoryService.DirectoryEntryPropertyDirectoryEntry">
<End Role="DirectoryEntryProperty" EntitySet="DirectoryEntryProperties" />
<End Role="DirectoryEntry" EntitySet="DirectoryEntries" />
</AssociationSet>
</EntityContainer>
<EntityType Name="DirectoryEntry">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="Name" Nullable="false" />
<Property Type="String" Name="Type" Nullable="false" />
<NavigationProperty Name="Properties" Relationship="DirectoryService.DirectoryEn开发者_StackOverflow社区tryPropertyDirectoryEntry" FromRole="DirectoryEntry" ToRole="DirectoryEntryProperty" ib10:Tag="DirectoryEntryPropertyCollectionHelper" />
</EntityType>
<EntityType Name="DirectoryEntryProperty">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<NavigationProperty Name="DirectoryEntry" Relationship="DirectoryService.DirectoryEntryPropertyDirectoryEntry" FromRole="DirectoryEntryProperty" ToRole="DirectoryEntry" />
<Property Type="Int32" Name="DirectoryEntryId" Nullable="false" />
<Property Type="String" Name="Key" Nullable="false" />
<Property Type="String" Name="Value" Nullable="false" />
</EntityType>
<Association Name="DirectoryEntryPropertyDirectoryEntry">
<End Type="DirectoryService.DirectoryEntryProperty" Role="DirectoryEntryProperty" Multiplicity="*" />
<End Type="DirectoryService.DirectoryEntry" Role="DirectoryEntry" Multiplicity="1" />
<ReferentialConstraint>
<Principal Role="DirectoryEntry">
<PropertyRef Name="Id" />
</Principal>
<Dependent Role="DirectoryEntryProperty">
<PropertyRef Name="DirectoryEntryId" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
I'm not sure if this has been done before, but it's quite easy to accomplish what you are after using the T4 engine and reflection.
I don't know if I correctly understood what you're trying to do, but, I'd suggest you take a look at the MetadataType attribute. That is what I use in the RIA services... can then use reflection to look for the metadata properties you want...
I hope this is at least close to what you're looking for :)
精彩评论