【问题标题】:Silverlight + Entity framework code first + DomainService + partial classSilverlight + Entity 框架代码优先 + DomainService + 部分类
【发布时间】:2013-01-14 15:31:52
【问题描述】:

我与实体有 Silverlight 项目 这是我的餐桌课

 public class OrderHeader : INotifyPropertyChanged
{
    public string OrderId{get;set;}
}

我有

 public class DataBaseContext : DbContext
{
    public DbSet<OrderHeader> OrderHeaders { get; set; }

}

我有域名服务

[EnableClientAccess()]
public class ShopService : DomainService
{
    [Invoke]
    public OrderHeader GetOrderHeader()
    {
        using (var db = new DataBaseContext())
        {
            return db.OrderHeader.FirstOrdefault(o=>o.OrderId == "123";
         }
    }
}

在我的 SL 应用程序中,我想使用 OrderHeader 作为部分类。像

public partial class OrderHeader
{
   public DateTime LoadDate{get;set;}
}

private void LoadData()
{
  ShopServiceContext context = new ShopServiceContext();
  context.GetOrderHeader(OrderLoaded, null)
}
private void OrderLoaded(InvokeOperation<OrderHeader> result)
{
   var loadedOrder = result.Value; //load order this is my partial class from SL app
   loadedOrder.Loaddate = DateTime.Now;
} 

我可以像这样创造一些想法吗?

【问题讨论】:

    标签: silverlight entity-framework ef-code-first partial-classes


    【解决方案1】:

    是的。首先创建一个新的 Silverlight 类库来保存 Silverlight 项目的实体。然后,使用“添加为链接”将您的实体(OrderHeader)添加到此项目中。

    对于仅在 Silverlight 端可用的属性(或其他代码部分),您可以使用如下编译器指令:

    #if SILVERLIGHT
        // Do silverlight stuff 
        public DateTime LoadDate{get;set;}
    #endif
    

    在此之后,您的实体将根据 Silverlight 项目的不同条件进行编译。

    【讨论】:

    • 在setter上你不需要通知属性已经改变吗?
    • 如果您将此属性绑定到 Silverlight 组件或在 Silverlight 端进行验证,那么您需要触发 notifypropertychanged。你的属性可能是这样的:私有字符串_title;公共字符串标题 { 获取 { 返回 _title; } 设置 { if (_title != value) { _title = value; this.OnPropertyChanged(new PropertyChangedEventArgs("Title")); } } }`
    • 我使用 Notufy。我是写简单的例子。我在哪里可以找到“添加为链接”选项(VS 2012)
    • 右键项目->添加->现有项目。您可以在附加到添加按钮的组合中看到“添加为链接”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多