Added a Database and an Entity Model to my DAL Project.
got an error :
Error :
+ base {"Schema specified is not valid. Errors: \r\nMultiple types with the name 'TblRecord' exist in the EdmItemCollection in different namespaces. Convention based mapping requires unique names without regard to namespace in the EdmItemCollection."} System.Data.EntityException {System.Data.MetadataException}
The Edmx File
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Xz.Business.Matches.Store" Alias="Self" Provider="System.Data.SqlServerCe.4.0" ProviderManifestToken="4.0" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<EntityContainer Name="XzBusinessMatchesStoreContainer">
<EntitySet Name="Records" EntityType="Xz.Business.Matches.Store.Records" store:Type="Tables" />
</EntityContainer>
<EntityType Name="Records">
<Key>
<PropertyRef Name="Record" />
</Key>
<Property Name="Record" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="Relations" Type="nvarchar" MaxLength="450" />
</EntityType>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="Xz.Business.Matches" Alias="Self" p1:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityContainer Name="RecordzEntities" p1:LazyLoadingEnabled="true">
<EntitySet Name="Records" EntityType="Xz.Business.Matches.TblRecord" />
</EntityContainer>
<EntityType Name="TblRecord">
<Key>
<PropertyRef Name="Record" />
</Key>
<Property Name="Record" Type="String" Nullable="false" MaxLength="100" Unicode="true" FixedLength="false" />
<Property Name="Relations" Type="String" MaxLength="450" Unicode="true" FixedLength="false" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
<EntityContainerMapping StorageEntityContainer="XzBusinessMatchesStoreContainer" CdmEntityContainer="RecordzEntities">
<EntitySetMapping Name="Records">
<EntityTypeMapping TypeName="Xz.Business.Matches.TblRecord">
<MappingFragment StoreEntitySet="Records">
<ScalarProperty Name="Record" ColumnName="Record" />
<ScalarProperty Name="Relations" ColumnName="Relations" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
<Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</Connection>
<Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
<DesignerProperty Name="EnablePluralization" Value="True" />
<DesignerProperty Name="IncludeForeignKeysInModel" Value="True" />
<DesignerProperty Name="CodeGenerationStrategy" Value="None" />
</DesignerInfoPropertySet>
</Options>
<!-- Diagram content (shape and connector positions) -->
<Diagrams></Diagrams>
</Designer>
</edmx:Edmx>
Names & Namespaces :
Project Name : Business.Matches
Default Namespace : Xz.Business.Matches
I added an Entity Model with this definitions :
Model : RecordzModel
Context : RecordzContext
Model Namespace : Xz.Business.Matches
You seem to have more than one class in your project that is called 'TblRecord'. Namespaces are ignored when matching your types to EDM types for POCO types and therefore if you have more than one class with the same name it is ambiguous and causes the exception.
Related
I keep getting the error "Error 4: Could not load file or assembly 'MySql.Data.EntityFramework, Version=8.0.25.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)" when trying to compile my csproj. Any help would be greatly appreciated
here is my ucsdb.edmx
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="ucsdbModel.Store" Provider="MySql.Data.MySqlClient"
ProviderManifestToken="5.6" Alias="Self"
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<EntityType Name="Clan">
<Key>
<PropertyRef Name="ClanId" />
</Key>
<Property Name="ClanId" Type="bigint" Nullable="false" />
<Property Name="LastUpdateTime" Type="datetime" Precision="0" Nullable="false" />
<Property Name="Data" Type="text" Nullable="false" />
</EntityType>
<EntityType Name="Player">
<Key>
<PropertyRef Name="PlayerId" />
</Key>
<Property Name="PlayerId" Type="bigint" Nullable="false" />
<Property Name="Avatar" Type="text" Nullable="false" />
<Property Name="GameObjects" Type="text" Nullable="false" />
</EntityType>
<EntityContainer Name="ucsdbModelStoreContainer">
<EntitySet Name="Clan" EntityType="Self.Clan" Schema="ucsdb" store:Type="Tables" />
<EntitySet Name="Player" EntityType="Self.Player" Schema="ucsdb" store:Type="Tables" />
</EntityContainer>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="ucsdbModel" Alias="Self" annotation:UseStrongSpatialTypes="false"
xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityType Name="Clan">
<Key>
<PropertyRef Name="ClanId" />
</Key>
<Property Name="ClanId" Type="Int64" Nullable="false" />
<Property Name="LastUpdateTime" Type="DateTime" Nullable="false" />
<Property Name="Data" Type="String" Nullable="false" MaxLength="65535"
FixedLength="false" Unicode="false" />
</EntityType>
<EntityType Name="Player">
<Key>
<PropertyRef Name="PlayerId" />
</Key>
<Property Name="PlayerId" Type="Int64" Nullable="false" />
<Property Name="Avatar" Type="String" Nullable="false" MaxLength="65535"
FixedLength="false" Unicode="false" />
<Property Name="GameObjects" Type="String" Nullable="false" MaxLength="65535"
FixedLength="false"
Unicode="false" />
</EntityType>
<EntityContainer Name="ucsdbEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Clan" EntityType="Self.Clan" />
<EntitySet Name="Player" EntityType="Self.Player" />
</EntityContainer>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S"
xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs
">
<EntityContainerMapping
StorageEntityContainer="ucsdbModelStoreContainer"
CdmEntityContainer="ucsdbEntities">
<EntitySetMapping Name="Clan">
<EntityTypeMapping TypeName="ucsdbModel.Clan">
<MappingFragment StoreEntitySet="Clan">
<ScalarProperty Name="ClanId"
ColumnName="ClanId" />
<ScalarProperty Name="LastUpdateTime"
ColumnName="LastUpdateTime" />
<ScalarProperty Name="Data"
ColumnName="Data" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<EntitySetMapping Name="Player">
<EntityTypeMapping
TypeName="ucsdbModel.Player">
<MappingFragment StoreEntitySet="Player">
<ScalarProperty Name="PlayerId"
ColumnName="PlayerId" />
<ScalarProperty Name="GameObjects"
ColumnName="GameObjects" />
<ScalarProperty Name="Avatar"
ColumnName="Avatar" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW
HERE) -->
<Designer
xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
<Connection>
<DesignerInfoPropertySet>
<DesignerProperty
Name="MetadataArtifactProcessing"
Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</Connection>
<Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild"
Value="true" />
<DesignerProperty Name="EnablePluralization"
Value="false" />
<DesignerProperty Name="IncludeForeignKeysInModel"
Value="false" />
<DesignerProperty Name="UseLegacyProvider"
Value="false" />
<DesignerProperty Name="CodeGenerationStrategy"
Value="None" />
</DesignerInfoPropertySet>
</Options>
<!-- Diagram content (shape and connector positions) -
->
<Diagrams></Diagrams>
</Designer>
</edmx:Edmx>
Need to use MySql.Data.EntityFramework.8.0.25, but the program does not have this dll file or use another version.
Click on the project's directory, right-click References and open Manage NuGet Packages.
Search for MySql.Data.EntityFramework to see Version. I hope I can help you.
I am exploring OData.
I have been following Working with OData - Integrating an Existing ASP.NET Core 3.x API by Referbruv!
My EdmModel follows his and is as follows:
internal static IEdmModel GetEdmModel() {
// create OData builder instance
var builder = new ODataConventionModelBuilder();
// CLIENTS
// map the entities set which are the types returned from the endpoint onto the OData pipeline
// the string parameter is the name of the controller
// which supplies the data of type Client entity model in this case
builder.EntitySet<ClientIndexDto>("ODataClient").EntityType.HasKey(x => x.id);
builder.EntitySet<ClientDetailsDto>("ODataClientDetails").EntityType.HasKey(x => x.id);
// configure a function onto the builder, AllClients
// which is same as the name provided in the ODataRoute
builder.Function("AllClients")
.ReturnsCollectionFromEntitySet<ClientIndexDto>("ODataClient");
builder.Function("ClientById")
.ReturnsCollectionFromEntitySet<ClientDetailsDto>("ODataClientDetails")
.Parameter<int>("id");
return builder.GetEdmModel();
}
The section:
builder.Function("ClientById")
.ReturnsCollectionFromEntitySet<ClientDetailsDto>("ODataClientDetails")
.Parameter<int>("id");
Deals with getting a client by Id.
For completeness I have in My StartUp.cs file the following for OData:
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
endpoints.Select().Filter().OrderBy().Expand().Count().MaxTop(10);
endpoints.MapODataRoute("odata", "odata", GetEdmModel());
});
The $metadata returned is as follows:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="JobsLedger.MODELS.API.App.Client" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="ClientIndexDto">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="Edm.Int32" Nullable="false" />
<Property Name="ClientNo" Type="Edm.String" />
<Property Name="Active" Type="Edm.Boolean" Nullable="false" />
<Property Name="ClientFirstName" Type="Edm.String" />
<Property Name="ClientLastName" Type="Edm.String" />
<Property Name="Company" Type="Edm.Boolean" Nullable="false" />
<Property Name="CompanyName" Type="Edm.String" />
<Property Name="MobilePhone" Type="Edm.String" />
<Property Name="IsWarrantyCompany" Type="Edm.Boolean" Nullable="false" />
<Property Name="JobsCount" Type="Edm.String" />
</EntityType>
<EntityType Name="ClientDetailsDto">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="Edm.Int32" Nullable="false" />
<Property Name="ClientNo" Type="Edm.String" />
<Property Name="Company" Type="Edm.Boolean" Nullable="false" />
<Property Name="IsWarrantyCompany" Type="Edm.Boolean" Nullable="false" />
<Property Name="CompanyName" Type="Edm.String" />
<Property Name="ClientFirstName" Type="Edm.String" />
<Property Name="ClientLastName" Type="Edm.String" />
<Property Name="MobilePhone" Type="Edm.String" />
<Property Name="DeActivated" Type="Edm.String" />
<Property Name="CreatedOn" Type="Edm.String" />
<Property Name="CreatedBy" Type="Edm.String" />
<Property Name="ModifiedOn" Type="Edm.String" />
<Property Name="ModifiedBy" Type="Edm.String" />
<Property Name="SuburbId" Type="Edm.Int32" Nullable="false" />
<NavigationProperty Name="Address" Type="JobsLedger.MODELS.Common.Address.AddressDto" />
<NavigationProperty Name="ClientJobs" Type="Collection(JobsLedger.MODELS.API.App.Job.ClientJobDto)" />
<NavigationProperty Name="ClientNotes" Type="Collection(JobsLedger.MODELS.Common.Notes.ClientNoteDto)" />
</EntityType>
</Schema>
<Schema Namespace="JobsLedger.MODELS.Common.Address" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="AddressDto">
<Key>
<PropertyRef Name="Address1" />
</Key>
<Property Name="Address1" Type="Edm.String" Nullable="false" />
<Property Name="Address2" Type="Edm.String" />
<Property Name="SuburbId" Type="Edm.Int32" Nullable="false" />
<Property Name="SuburbName" Type="Edm.String" />
<Property Name="StateShortName" Type="Edm.String" />
<Property Name="Postcode" Type="Edm.String" />
</EntityType>
</Schema>
<Schema Namespace="JobsLedger.MODELS.API.App.Job" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="ClientJobDto">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="JobNo" Type="Edm.String" />
<Property Name="AgentJobNo" Type="Edm.String" />
<Property Name="Type" Type="Edm.String" />
<Property Name="Status" Type="Edm.String" />
<Property Name="WarrantyCompany" Type="Edm.String" />
<Property Name="NumberOfVisits" Type="Edm.String" />
<Property Name="CreatedOn" Type="Edm.String" />
<Property Name="CreatedBy" Type="Edm.String" />
<Property Name="ModifiedOn" Type="Edm.String" />
<Property Name="ModifiedBy" Type="Edm.String" />
<NavigationProperty Name="JobNotes" Type="Collection(JobsLedger.MODELS.Common.Notes.JobNoteDto)" />
<NavigationProperty Name="JobVisits" Type="Collection(JobsLedger.MODELS.API.App.Job.JobVisitDto)" />
</EntityType>
<EntityType Name="JobVisitDto">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="DateCreated" Type="Edm.String" />
<Property Name="VisitDate" Type="Edm.String" />
<Property Name="StartTime" Type="Edm.String" />
<Property Name="EndTime" Type="Edm.String" />
</EntityType>
</Schema>
<Schema Namespace="JobsLedger.MODELS.Common.Notes" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="JobNoteDto">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="JobId" Type="Edm.Int32" Nullable="false" />
<Property Name="Details" Type="Edm.String" />
<Property Name="NoteType" Type="Edm.String" />
<Property Name="CreatedOnDate" Type="Edm.String" />
<Property Name="CreatedOnTime" Type="Edm.String" />
<Property Name="CreatedBy" Type="Edm.String" />
<Property Name="ModifiedOnDate" Type="Edm.String" />
<Property Name="ModifiedOnTime" Type="Edm.String" />
<Property Name="ModifiedBy" Type="Edm.String" />
</EntityType>
<EntityType Name="ClientNoteDto">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="Edm.Int32" Nullable="false" />
<Property Name="Details" Type="Edm.String" />
<Property Name="NoteType" Type="Edm.String" />
<Property Name="CreatedOnDate" Type="Edm.String" />
<Property Name="CreatedOnTime" Type="Edm.String" />
<Property Name="CreatedBy" Type="Edm.String" />
<Property Name="ModifiedOnDate" Type="Edm.String" />
<Property Name="ModifiedOnTime" Type="Edm.String" />
<Property Name="ModifiedBy" Type="Edm.String" />
</EntityType>
</Schema>
<Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Function Name="AllClients">
<ReturnType Type="Collection(JobsLedger.MODELS.API.App.Client.ClientIndexDto)" />
</Function>
<Function Name="ClientById">
<Parameter Name="id" Type="Edm.Int32" Nullable="false" />
<ReturnType Type="Collection(JobsLedger.MODELS.API.App.Client.ClientDetailsDto)" />
</Function>
<EntityContainer Name="Container">
<EntitySet Name="ODataClient" EntityType="JobsLedger.MODELS.API.App.Client.ClientIndexDto" />
<EntitySet Name="ODataClientDetails" EntityType="JobsLedger.MODELS.API.App.Client.ClientDetailsDto" />
<FunctionImport Name="AllClients" Function="Default.AllClients" EntitySet="ODataClient" IncludeInServiceDocument="true" />
<FunctionImport Name="ClientById" Function="Default.ClientById" EntitySet="ODataClientDetails" IncludeInServiceDocument="true" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
My Controller is as follows:
using JobsLedger.API.Controllers.API.App.Interfaces;
using JobsLedger.API.ControllerServices.API.App.ClientService.Interfaces;
using JobsLedger.DATA;
using JobsLedger.DATA.Repositories.Interfaces;
using JobsLedger.MODELS.API.App.Client;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
namespace JobsLedger.API.Controllers.API.App {
[Route("api/[controller]")]
[ApiController]
[Authorize(Roles = "TenantAdmin,Admin,Employee")]
public class ODataClientController : ODataController, IClientController {
private readonly IClientServices _clientServices;
public ODataClientController( IClientServices clientServices) {
_clientServices = clientServices;
}
[HttpGet]
[EnableQuery()]
[ODataRoute("AllClients()")]
public IActionResult Get()
{
return Ok(_clientServices.GetAllClientsAsDto());
}
[HttpGet]
[EnableQuery]
[ODataRoute("ClientById(id={id})")]
public IActionResult Get(int id) {
return Ok(_clientServices.GetClient(id));
}
I want to get a client with an ID of "5" so I use the following get command in Postman:
https://localhost:44301/odata/ClientById(id=5)?
This is as the tutorial indicates - with brackets around the "id=5"
It gets to the endpoint but the id is set to "0".
Given my EdmModel, the #MetaData, and the controller action for a single Id why, when I send a GET command with an (Id=5) I get an Id=0? What do I need to do to fix this?
In the article comments, Referbruv also mentions issue with routing in .net core 3.1. So, you can try it in 3.0 and if it works there, it is the issue with 3.1.
If you can do it with id= then based on the attr-routing this should work.
[ODataRoute("ClientById({id})")]
I'm trying to add a bound function to my Web API 2.2 OData v4 controller, but it returns 404 with the error message:
{
"error":{
"code":"","message":"No HTTP resource was found that matches the request URI 'http://localhost:2390/Hierarchies('300924834')/FullHierarchy'.","innererror":{
"message":"No routing convention was found to select an action for the OData path with template '~/entityset/key/unresolved'.","type":"","stacktrace":""
}
}
}
This is my method on the HierarchiesController:
[HttpGet]
public IHttpActionResult FullHierarchy([FromODataUri]string key)
{
return Ok(new Hierarchy());
}
I tried to add the attribute [ODataRoute("Hierarchies({key}/FullHierarchy)")]on my method but then I got the following error on the line GlobalConfiguration.Configure(WebApiConfig.Register);when first running the application:
The path template 'Hierarchies({key}/FullHierarchy)' on the action 'FullHierarchy' in controller 'Hierarchies' is not a valid OData path template. Bad Request - Error in query syntax.
This is my configuration in WebApiConfig:
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.Namespace = ""; // tried with and without this line.
var hierachyType = builder.EntitySet<Hierarchy>("Hierarchies").EntityType;
hierachyType.Function("FullHierarchy").Returns<Hierarchy>();
Note that bound functions to collections don't work as well, but unbound functions do work.
This is how my $metadata looks like:
<?xml version="1.0" encoding="UTF-8"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<edmx:DataServices>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Entities.Models">
<EntityType Name="Hierarchy">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Name" Type="Edm.String" />
<Property Name="Id" Type="Edm.String" Nullable="false" />
<NavigationProperty Name="Policy" Type="Entities.Models.Policy" />
<NavigationProperty Name="Nodes" Type="Collection(Entities.Models.HierarchyNode)" />
</EntityType>
<EntityType Name="Node" OpenType="true">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Name" Type="Edm.String" />
<Property Name="Id" Type="Edm.String" Nullable="false" />
</EntityType>
<EntityType Name="Site" BaseType="Entities.Models.Node" OpenType="true">
<Property Name="Address" Type="Edm.String" />
</EntityType>
<EntityType Name="Policy">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.String" Nullable="false" />
<Property Name="Name" Type="Edm.String" />
<NavigationProperty Name="Rules" Type="Collection(Entities.Rules.Rule)" />
</EntityType>
<EntityType Name="HierarchyNode">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.String" Nullable="false" />
<Property Name="Name" Type="Edm.String" />
<NavigationProperty Name="Nodes" Type="Collection(Entities.Models.HierarchyNode)" />
</EntityType>
<EntityType Name="Building" BaseType="Entities.Models.Node" OpenType="true">
<Property Name="StaffCount" Type="Edm.Int32" Nullable="false" />
</EntityType>
<EntityType Name="Source" BaseType="Entities.Models.Node" OpenType="true">
<Property Name="GroupByOperation" Type="Edm.String" />
<Property Name="Factor" Type="Edm.Int32" Nullable="false" />
</EntityType>
</Schema>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Entities.Rules">
<EntityType Name="Rule" Abstract="true">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.String" Nullable="false" />
<Property Name="Name" Type="Edm.String" />
<Property Name="NodeType" Type="Edm.String" />
</EntityType>
<EntityType Name="ParentChildRule" BaseType="Entities.Rules.Rule">
<Property Name="AllowedParentsTypes" Type="Edm.String" />
<Property Name="ForbiddenParentsTypes" Type="Edm.String" />
</EntityType>
<EntityType Name="PredicateRule_1OfTNode" BaseType="Entities.Rules.Rule">
<Property Name="Predicate" Type="System.Linq.Expressions.Expression_1OfFunc_2OfTNode_String" />
</EntityType>
<EntityType Name="PropertyRule" BaseType="Entities.Rules.Rule">
<Property Name="RequiredProperties" Type="Edm.String" />
</EntityType>
</Schema>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="System.Linq.Expressions">
<ComplexType Name="Expression_1OfFunc_2OfTNode_String">
<Property Name="Parameters" Type="Collection(System.Linq.Expressions.ParameterExpression)" />
</ComplexType>
<ComplexType Name="ParameterExpression" />
</Schema>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Function Name="FullHierarchy" IsBound="true">
<Parameter Name="bindingParameter" Type="Entities.Models.Hierarchy" />
<ReturnType Type="Entities.Models.Hierarchy" />
</Function>
<EntityContainer Name="Container">
<EntitySet Name="Hierarchies" EntityType="Entities.Models.Hierarchy" />
<EntitySet Name="Nodes" EntityType="Entities.Models.Node" />
<EntitySet Name="Sites" EntityType="Entities.Models.Site" />
<EntitySet Name="Rules" EntityType="Entities.Rules.Rule" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
There are several things:
The schema needs to have a namespace. I.e. builder.Namespace = ""; needs to be removed or replaced with a non-empty string.
The function needs to be invoked by it's fully qualified name. I.e. The request URL needs to be http://localhost:2390/Hierarchies('300924834')/<namespace>.FullHierarchy in which <namespace> should be the default namespace or the one you specify in item #1.
As a function bound to a single entity, writing the controller method like this is adequate:
[HttpGet]
public IHttpActionResult FullHierarchy()
{
return Ok(new Hierarchy());
}
I don't know the exact reason. I'd guess the convention model builder helps you with such routing.
It works for me, and following are my test function details:
WebApiConfig:
builder.EntityType<Product>().Function("SomeFunction").Returns<string>();
In the ProductsController:
[HttpGet]
public IHttpActionResult SomeFunction()
{
return Ok("Some");
}
The request:
GET http://localhost:54017/Products(1)/Default.SomeFunction
I know the entity frame work does not allow you to generate a model from a database using non primary unique keys as a Foreign Key association. Can I modify the EDMX manually? If so, can someone provide me an example or reference? If not, are there any other possibilities?
Easiest Example:
Here is the DDL for the tables. You will notice I have a foreign Key from PersonType.TypeCode to Person.TypeCode
CREATE TABLE [dbo].[PersonType](
[PersonTypeId] [int] NOT NULL,
[TypeCode] [varchar](10) NOT NULL,
[TypeDesc] [varchar](max) NULL,
CONSTRAINT [PK_PersonType] PRIMARY KEY CLUSTERED
([PersonTypeId] ASC)
CONSTRAINT [UK_PersonType] UNIQUE NONCLUSTERED
([TypeCode] ASC)
)
CREATE TABLE [dbo].[Person](
[PersonId] [int] NOT NULL,
[TypeCode] [varchar](10) NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
([PersonId] ASC)
)
ALTER TABLE [dbo].[Person] WITH CHECK ADD CONSTRAINT [FK_Person_PersonType] FOREIGN KEY([TypeCode])
REFERENCES [dbo].[PersonType] ([TypeCode])
ALTER TABLE [dbo].[Person] CHECK CONSTRAINT [FK_Person_PersonType]
Here is the EDMX Generated
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="testModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="testModelStoreContainer">
<EntitySet Name="Person" EntityType="testModel.Store.Person" store:Type="Tables" Schema="dbo" />
<EntitySet Name="PersonType" EntityType="testModel.Store.PersonType" store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="Person">
<Key>
<PropertyRef Name="PersonId" />
</Key>
<Property Name="PersonId" Type="int" Nullable="false" />
<Property Name="TypeCode" Type="varchar" Nullable="false" MaxLength="10" />
</EntityType>
<!--Errors Found During Generation:
warning 6035: The relationship 'FK_Person_PersonType' has columns that are not part of the key of the table on the primary side of the relationship. The relationship was excluded.
-->
<EntityType Name="PersonType">
<Key>
<PropertyRef Name="PersonTypeId" />
</Key>
<Property Name="PersonTypeId" Type="int" Nullable="false" />
<Property Name="TypeCode" Type="varchar" Nullable="false" MaxLength="10" />
<Property Name="TypeDesc" Type="varchar(max)" />
</EntityType>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="testModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="testEntities">
<EntitySet Name="People" EntityType="testModel.Person" />
<EntitySet Name="PersonTypes" EntityType="testModel.PersonType" />
</EntityContainer>
<EntityType Name="Person">
<Key>
<PropertyRef Name="PersonId" />
</Key>
<Property Name="PersonId" Type="Int32" Nullable="false" />
<Property Name="TypeCode" Type="String" Nullable="false" MaxLength="10" Unicode="false" FixedLength="false" />
</EntityType>
<EntityType Name="PersonType">
<Key>
<PropertyRef Name="PersonTypeId" />
</Key>
<Property Name="PersonTypeId" Type="Int32" Nullable="false" />
<Property Name="TypeCode" Type="String" Nullable="false" MaxLength="10" Unicode="false" FixedLength="false" />
<Property Name="TypeDesc" Type="String" MaxLength="Max" Unicode="false" FixedLength="false" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
<EntityContainerMapping StorageEntityContainer="testModelStoreContainer" CdmEntityContainer="testEntities">
<EntitySetMapping Name="People"><EntityTypeMapping TypeName="testModel.Person"><MappingFragment StoreEntitySet="Person">
<ScalarProperty Name="PersonId" ColumnName="PersonId" />
<ScalarProperty Name="TypeCode" ColumnName="TypeCode" />
</MappingFragment></EntityTypeMapping></EntitySetMapping>
<EntitySetMapping Name="PersonTypes"><EntityTypeMapping TypeName="testModel.PersonType"><MappingFragment StoreEntitySet="PersonType">
<ScalarProperty Name="PersonTypeId" ColumnName="PersonTypeId" />
<ScalarProperty Name="TypeCode" ColumnName="TypeCode" />
<ScalarProperty Name="TypeDesc" ColumnName="TypeDesc" />
</MappingFragment></EntityTypeMapping></EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
I have tried to modify the EDMX to create the navigation propery between personType and Person but have been unsuccessful. I just figured I could create the association manually some how. Any help would be appreciated.
Unfortunately as of now there is no way to define an association on a candidate key (i.e. PersonType.TypeCode). because In EF (3.5 and 4.0) FKs MUST point to Primary Keys.
According to Alex James from his post here, this is something the EF team are considering for the next version.
One of my Entity Framework Models contains a MS-SQL View. The View is called User and contains various properties for users.
When I right click on the model and select Update Model from Database, I get the following error:
"An exception of type 'System.InvalidOperationException' occurred while attempting to update from the database. The exception message is: 'A model generation extension made changes to the model generated from the database that were not valid'.
This occurs in any model we have that uses a View in Entity Framework 4.0. It's getting very tedious to delete and recreate the model every time some part of the database structure changes. Is there anyway to make "Update Model from Database" work when my model contains a View?
If it makes a difference, this is a readonly View.
EDIT: I forgot to mention, this is a cross-database View. That seems to be where the problem occurs. A regular view (within the same database) works fine.
Here's a my sample EDMX file. I'm not sure if it will be useful or not.
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<edmx:Runtime>
<edmx:StorageModels>
<Schema Namespace="SignupsModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="SignupsModelStoreContainer">
<EntitySet Name="Users" EntityType="SignupsModel.Store.Users" store:Type="Views" Schema="dbo">
</EntitySet>
</EntityContainer>
<EntityType Name="Users">
<Key>
<PropertyRef Name="userID" />
</Key>
<Property Name="userID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="userName" Type="varchar" Nullable="false" MaxLength="50" />
<Property Name="campusID" Type="varchar" Nullable="false" MaxLength="7" />
<Property Name="firstName" Type="varchar" Nullable="false" MaxLength="30" />
<Property Name="middleInitial" Type="varchar" Nullable="false" MaxLength="1" />
<Property Name="lastName" Type="varchar" Nullable="false" MaxLength="30" />
</EntityType>
</Schema>
</edmx:StorageModels>
<edmx:ConceptualModels>
<Schema Namespace="SignupsModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="Entities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Users" EntityType="SignupsModel.User" />
</EntityContainer>
<EntityType Name="User">
<Key>
<PropertyRef Name="userID" />
</Key>
<Property Name="userID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Name="userName" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
<Property Name="campusID" Type="String" Nullable="false" MaxLength="7" Unicode="false" FixedLength="false" />
<Property Name="firstName" Type="String" Nullable="false" MaxLength="30" Unicode="false" FixedLength="false" />
<Property Name="middleInitial" Type="String" Nullable="false" MaxLength="1" Unicode="false" FixedLength="false" />
<Property Name="lastName" Type="String" Nullable="false" MaxLength="30" Unicode="false" FixedLength="false" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
<EntityContainerMapping StorageEntityContainer="SignupsModelStoreContainer" CdmEntityContainer="Entities">
<EntitySetMapping Name="Users"><EntityTypeMapping TypeName="SignupsModel.User"><MappingFragment StoreEntitySet="Users">
<ScalarProperty Name="userID" ColumnName="userID" />
<ScalarProperty Name="userName" ColumnName="userName" />
<ScalarProperty Name="campusID" ColumnName="campusID" />
<ScalarProperty Name="firstName" ColumnName="firstName" />
<ScalarProperty Name="middleInitial" ColumnName="middleInitial" />
<ScalarProperty Name="lastName" ColumnName="lastName" />
</MappingFragment></EntityTypeMapping></EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
I think I tracked down the issue. A while back, I guess I installed a Visual Studio addon called "Edmx Views Processor". I thought I had uninstalled it. Removing the addon seems to solve the issue.
The extension in question is located here:
http://visualstudiogallery.msdn.microsoft.com/en-us/d9b76b5d-d45c-4e79-8d28-31444be582de