2017-07-17 33 views
0

我創建使用的Visual Studio 2015年與「EF設計師從數據庫」如何創建C#的OData V4實體關係

我有2實體的OData v4的項目,它們是SQL視圖我從Microsoft SQL Server 2015年創建的,

我可以用簡單的OData URI 本地主機中的數據:交/的OData/BookingInfoes( 'BookingId')或 localhsot:交/的OData/Timemarkers( 'BookingId')

我的問題是一個BookingInfo涉及到許多時間標記項目和 我無法使用命令?$ expand,i tri海關在谷歌很多的方法,但是仍然沒有運氣,

我想是這樣

{BookingID:123,BookingDate:"sss",[TimeMarker:[{id:1,info:"sss"},{id:2,info:"balh bla"},{id:3,info:"foo foof oo"}]]} 

請大家幫忙,這個卡我一個星期,我不知道該如何處理。

entiy BookingInfo

namespace wcod.Models 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class BookingInfo 
    { 
     public BookingInfo() 
     { 
      var TimeMarkerList = new List<TimeMarker>(); 
     } 
     public string BookingID { get; set; } 
     public string BookingDate { get; set; } 
     public string BookName { get; set; } 
     public string ChiBookName { get; set; } 
     public string VideoSource_1500 { get; set; } 
     public string VideoSource_300 { get; set; } 
     public string VideoSource_Archive { get; set; } 
     public string MobileVideoSource { get; set; } 
     public string languages { get; set; } 
     public string SeekTime { get; set; } 
     public Nullable<int> IsVideo { get; set; } 
     public string AvailableLangs { get; set; } 
     public Nullable<int> StatusMarkers { get; set; } 
     public string BookRoomID { get; set; } 
     public Nullable<System.DateTime> StartTime { get; set; } 
     public Nullable<int> nexturl { get; set; } 
     public string StrBookingStartTime { get; set; } 
     public string BookomgStatus { get; set; } 

     public virtual ICollection<TimeMarker> TimeMarkers { get; set; } 
    } 
} 

TimeMarker也從SQL視圖生成

namespace wcod.Models 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class TimeMarker 
    { 
     public Nullable<long> nrow { get; set; } 
     public Nullable<System.DateTime> StartTime { get; set; } 
     public Nullable<int> OrderNo { get; set; } 
     public string MeetingID { get; set; } 
     public string AgendaName { get; set; } 
     public string ChiAgendaName { get; set; } 
     public int AgendaCode { get; set; } 
     public string AgendaTime { get; set; } 
     public string SpeakerCode { get; set; } 
     public Nullable<int> MarkerID { get; set; } 
     public string AgendaRunningTime { get; set; } 
     public Nullable<bool> AllLangFail { get; set; } 
     public Nullable<bool> isLive { get; set; } 
     public string PopUpMsg { get; set; } 
     public Nullable<bool> HasVideo { get; set; } 
     public string TimeMarkerId { get; set; } 

     public virtual BookingInfo BookingInfo { get; set; } 
    } 
} 

WebApiConfig

using Newtonsoft.Json; 
using Newtonsoft.Json.Serialization; 
using System.Web.Http; 
using System.Web.Http.OData.Builder; 
using System.Web.Http.OData.Extensions; 
using System.Web.OData.Extensions; 
using wcod3.Models; 

namespace wcod3 
{ 
    public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      // Web API configuration and services 
      ODataModelBuilder builder = new ODataConventionModelBuilder(); 
      config.Count().Filter().OrderBy().Expand().Select().MaxTop(null); 

      var json = config.Formatters.JsonFormatter; 
      json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; 
      config.Formatters.Remove(config.Formatters.XmlFormatter); 

      config.Formatters.Remove(config.Formatters.XmlFormatter); 
      config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented; 
      config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
      // var json = config.Formatters.JsonFormatter;   
      builder.EntitySet<TimeMarker>("TimeMarkers"); 
      builder.EntitySet<MeetingInfo>("BookingInfoes"); 

      config.MapHttpAttributeRoutes(); 

      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 


      ); 

      config.Routes.MapODataServiceRoute("odata", "odata/v4", builder.GetEdmModel()); 
     } 
    } 
} 

這裏是我的BookingsController

繼承人是個ËTimeMarkersController

[EnableQuery] 
    public IQueryable<TimeMarker> GetTimeMarkers() 
    { 
     return db.TimeMarkers; 
    } 

    // GET: odata/TimeMarkers(5) 
    [EnableQuery] 
    public List<TimeMarker> GetTimeMarker([FromODataUri] string key) 
    { 
     // return SingleResult.Create(db.TimeMarkers.Where(timeMarker => timeMarker.MeetingID == key)); 
     // return List 
     return db.TimeMarkers.Where(timeMarker => timeMarker.MeetingID == key).ToList() ; 

    } 
    public List<MeetingInfo> GetMeetingInfo([FromODataUri] string key) 
    { 
     return db.MeetingInfoes.Where(mi => mi.MeetingID == key).ToList(); 
    } 

和packages.config

<?xml version="1.0" encoding="utf-8"?> 
<packages> 
    <package id="EntityFramework" version="6.1.3" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.OData" version="6.0.0" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" /> 
    <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" /> 
    <package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net452" /> 
    <package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net452" /> 
    <package id="Microsoft.Extensions.DependencyInjection" version="1.0.0" targetFramework="net452" /> 
    <package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.0.0" targetFramework="net452" /> 
    <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" /> 
    <package id="Microsoft.OData.Core" version="7.0.0" targetFramework="net452" /> 
    <package id="Microsoft.OData.Edm" version="7.0.0" targetFramework="net452" /> 
    <package id="Microsoft.Spatial" version="7.0.0" targetFramework="net452" /> 
    <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" /> 
    <package id="System.Spatial" version="5.6.0" targetFramework="net452" /> 
</packages> 






http://localhost:49347/odata/v4/TimeMarkers('id')/?$expand=BookingInfo 
http://localhost:49347/odata/v4/TimeMarkers('id')?$expand=BookingInfo 
http://localhost:49347/odata/v4/MeetingInfoes('id')/?$expand=TimeMarkers 
http://localhost:49347/odata/v4/MeetingInfoes('id')?$expand=TimeMarkers 

所有4網址顯示父內容只以上,我真的希望他們表現出一起。 請幫忙。

回答

0

我從來沒有使用odata與sql-view,但我認爲你需要添加一些信息給你模型:沒有明確的關係在BookingInfo和TimeMarker之間。

嘗試添加到瀏覽TimeMaker一個明確提及BookingInfo(前BookingInfoId),並以實體TimeMaker:

public partial class TimeMarker 
{ 
    public Nullable<long> nrow { get; set; } 
    public Nullable<System.DateTime> StartTime { get; set; } 
    public Nullable<int> OrderNo { get; set; } 
    public string MeetingID { get; set; } 
    public string AgendaName { get; set; } 
    public string ChiAgendaName { get; set; } 
    public int AgendaCode { get; set; } 
    public string AgendaTime { get; set; } 
    public string SpeakerCode { get; set; } 
    public Nullable<int> MarkerID { get; set; } 
    public string AgendaRunningTime { get; set; } 
    public Nullable<bool> AllLangFail { get; set; } 
    public Nullable<bool> isLive { get; set; } 
    public string PopUpMsg { get; set; } 
    public Nullable<bool> HasVideo { get; set; } 

    public int BookingInfoId { get; set; } 
    [ForeignKey("BookingInfoId ")] 
    public virtual BookingInfo BookingInfo { get; set; } 
} 

然後設置BookingInfo爲TimeMarkers的inverse屬性:

public partial class BookingInfo 
{ 
    [Required] 
    public string BookingID { get; set; } 
    public string BookingDate { get; set; } 
    public string MeetName { get; set; } 
    public string ChiMeetName { get; set; } 
    public string VideoSource_1500 { get; set; }   
    public string languages { get; set; }   
    public string AvailableLangs { get; set; } 
    public Nullable<int> StatusMarkers { get; set; } 
    public string MeetRoomID { get; set; } 
    public Nullable<System.DateTime> StartTime { get; set; } 
    public Nullable<int> nexturl { get; set; } 
    public string StrBookingStartTime { get; set; } 
    public string MeetomgStatus { get; set; } 

    [InverseProperty("BookingInfo")] 
    public virtual ICollection<TimeMarker> TimeMarkers { get; set; } 

} 

對不起,我不能嘗試,但我希望這可以幫助你。

+0

嘗試的http://本地主機:端口/的OData/V4/BookingInfoes(」 xxxx'),但只有BookingInfo顯示,沒有timeMarker dis玩然後我試着跟着這個[http://www.entityframeworktutorial.net/entity-relationships.aspx]仍然沒有 – user2285201

+0

你嘗試localhost:port/odata/v4/BookingInfoes('xxxx')?$ expand = TimeMarkers?如果您不在查詢字符串中添加$ expand,那麼您看到只有BookingInfo –

+0

我更新了該類,現在僅顯示父級內容。 – user2285201

0

我終於把工作做好,

我生成使用EF設計 使用GUI創建模型的關係的實體。EDMX,然後右鍵單擊 要創建的關係,並選擇加入協會 左「到‘的entityName’實體添加外鍵屬性」任何型號

*如果您自動更新模型,所有的註釋虛擬類將消失,我不知道爲什麼,最後我得到的關係做

http://localhost:49347/odata/v4/BookingInfoes('key')?$expand=TimeMarkers 

的package.config供您參考

<?xml version="1.0" encoding="utf-8"?> 
<packages> 
    <package id="EntityFramework" version="6.1.3" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.OData" version="6.0.0" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.WebApi.OData" version="5.3.1" targetFramework="net452" /> 
    <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" /> 
    <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" /> 
    <package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net452" /> 
    <package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net452" /> 
    <package id="Microsoft.Extensions.DependencyInjection" version="1.0.0" targetFramework="net452" /> 
    <package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.0.0" targetFramework="net452" /> 
    <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" /> 
    <package id="Microsoft.OData.Core" version="7.0.0" targetFramework="net452" /> 
    <package id="Microsoft.OData.Edm" version="7.0.0" targetFramework="net452" /> 
    <package id="Microsoft.Spatial" version="7.0.0" targetFramework="net452" /> 
    <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" /> 
    <package id="System.Spatial" version="5.6.0" targetFramework="net452" /> 
</packages> 
相關問題