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

发布于 2024-08-26 07:43:21 字数 193 浏览 6 评论 0原文

是否可以将 ASP.NET 动态数据与 SubSonic 3 一起使用来代替 Linq to SQL 类或实体框架?如果您使用 SubSonic 生成的上下文类,MetaModel.RegisterContext() 会引发异常。我想我记得在 SubSonic 3 发布之前遇到过 SubSonic/Dynamic Data 示例,但现在找不到了。有人能够让它发挥作用吗?

Is it possible to use ASP.NET Dynamic Data with SubSonic 3 in-place of Linq to SQL classes or the Entity Framework? MetaModel.RegisterContext() throws an exception if you use the context class that SubSonic generates. I thought I remembered coming across a SubSonic/Dynamic Data example back before SubSonic 3 was released but I can't find it now. Has anyone been able to get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的鱼塘能养鲲 2024-09-02 07:43:21

经过一些工作后,我昨晚刚刚在 Visual Studio 2010 中使用我的 SQLite 数据库让 Subsonic 3.0.0.4 ActiveRecord 工作起来,为了您的利益,我尝试记录此处所采取的步骤。

首先添加一个新项目 ->将 WCF 数据服务添加到您用来托管 Web 应用程序/Web 服务的项目,然后对其进行类似于下面的 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.DataSystem.Data 的引用.Services.Client,然后在顶部包含 使用 System.Data.Services使用 System.Data.Services.Common 的 using 语句ActiveRecord.tt 模板。

下一步是使用这篇博客文章中的 IUpdateable 部分类实现 http://blogs.msdn.com/aconrad/archive/2008/12/05/developing-an-astoria-data-provider-for-subsonic.aspx 并更改 public 分部类 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() 时,我不断收到错误

希望这有帮助 - 另外,我已经意识到 dbPINSDB 是一个真的坏我的亚音速数据库的名称;)

I just got Subsonic 3.0.0.4 ActiveRecord working last night in Visual Studio 2010 with my SQLite database after a little bit of work and I've tried to document the steps taken here for your benefit.

Start by adding a New Item -> WCF Data Service to the project you're using to host your webapp/webservices then modify it similar to my PinsDataService.svc.cs below:

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;
    }
}

At this point your Dynamic Data Service would probably be working if you matched all the database naming conventions perfectly but I didn't have that kind of luck. In my ActiveRecord.tt template I had to prepend the following two lines before the public partial class declarations:

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

I then added references to System.Data and System.Data.Services.Client followed by the inclusion of using statements for using System.Data.Services and using System.Data.Services.Common at the top of the ActiveRecord.tt template.

The next step was to use the IUpdateable partial class implementation from this blog post http://blogs.msdn.com/aconrad/archive/2008/12/05/developing-an-astoria-data-provider-for-subsonic.aspx and change the public partial class dbPINSDB : IUpdatable to match my subsonic DatabaseName declared in Settings.ttinclude

Then to consume the data in a separate client app/library I started by adding a 'Service Reference' named PinsDataService to the PinsDataService.svc from my client app and went to town:

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);

Note how I'm doing a Where query that returns only 1 object but I threw in the Take(1) and then ElementAt(0) because I kept getting errors when I tried to use SingleOrDefault() or First()

Hope this helps--also, I'm already aware that dbPINSDB is a really bad name for my Subsonic Database ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文