是否可以使用ASP.NET动态数据和SubSonic 3?

时间:2010-03-22 21:54:24

标签: subsonic subsonic3 dynamic-data

是否可以将ASP.NET动态数据与SubSonic 3一起使用到Linq to SQL类或实体框架?如果使用SubSonic生成的上下文类,MetaModel.RegisterContext()将引发异常。我以为我记得在SubSonic 3发布之前发现SubSonic / Dynamic Data的例子,但我现在找不到了。有没有人能够让这个工作?

1 个答案:

答案 0 :(得分:2)

我刚刚在Visual Studio 2010中使用我的SQLite数据库完成了Subsonic 3.0.0.4 ActiveRecord的工作,经过一些工作后我已经尝试记录为了您的利益而采取的步骤。

首先添加新商品 - > WCF数据服务到您用来托管webapp / webservices的项目,然后修改它,类似于下面的PinsDataService.svc.cs:

public class PinsDataService : DataService<PINS.Lib.dbPINSDB>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.UseVerboseErrors = true;
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

此时,如果您完全匹配所有数据库命名约定,那么您的动态数据服务可能会正常工作,但我没有那种运气。在我的 ActiveRecord.tt 模板中,我必须在公共分部类声明之前添加以下两行:

[DataServiceKey("<#=tbl.PrimaryKey #>")]
[IgnoreProperties("Columns")]
public partial class <#=tbl.ClassName#>: IActiveRecord {

然后我添加了对 System.Data System.Data.Services.Client 的引用,然后在中使用System.Data.Services包含using语句使用 ActiveRecord.tt 模板顶部的System.Data.Services.Common

下一步是使用此博客帖子http://blogs.msdn.com/aconrad/archive/2008/12/05/developing-an-astoria-data-provider-for-subsonic.aspx中的IUpdateable分部类实现,并更改public partial class dbPINSDB : IUpdatable以匹配在 Settings.ttinclude 中声明的亚音频DatabaseName

然后在单独的客户端应用程序/库中使用数据,我开始通过从我的客户端应用程序向PinsDataService.svc添加名为PinsDataService的“服务引用”并前往城镇:

PinsDataService.dbPINSDB PinsDb = 
 new PinsDataService.dbPINSDB(new Uri("http://localhost:1918/PinsDataService.svc/"));
PinsDataService.Alarm activeAlarm = 
   PinsDb.Alarms.Where(i => i.ID == myAA.Alarm_ID).Take(1).ElementAt(0);

注意我是如何做一个只返回1个对象的Where查询但是我扔了Take(1)然后是ElementAt(0),因为当我尝试使用SingleOrDefault()或First()时我一直收到错误/ p>

希望这会有所帮助 - 另外,我已经知道dbPINSDB是我的Subsonic数据库的 真的 坏名称;)