【问题标题】:How to add one-to-many relationships in Sql Azure .NET using code first?如何首先使用代码在 Sql Azure .NET 中添加一对多关系?
【发布时间】:2014-10-06 05:17:41
【问题描述】:

我正在开发 Azure 移动服务,我想在 Azure SQL 中创建一对多关系。例如,如果一个项目有很多区域。

我使用的代码是:

using Microsoft.WindowsAzure.Mobile.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AzureService.DataObjects
{
    public class Project : EntityData
    {
        public string Name { get; set; }
        public virtual ICollection<Area> Areas { get; set; }
    }
}

namespace AzureService.DataObjects
{
    public class Area : EntityData
    {
        public string Name { get; set; }
        public string ShapeFile { get; set; }

        public virtual ICollection<Scenario> Scenarios { get; set; }

        [ForeignKey("Id")]
        public virtual Project Project { get; set; }
    }
}

这是正确的做法吗?

【问题讨论】:

标签: sql azure entity-framework-4 azure-sql-database azure-mobile-services


【解决方案1】:

正确的实现是这样的:

using Microsoft.WindowsAzure.Mobile.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AzureService.DataObjects
{
    public class Project : EntityData
    {
        public string Name { get; set; }
        public virtual ICollection<Area> Areas { get; set; }
    }
}

namespace AzureService.DataObjects
{
    public class Area : EntityData
    {
        public string Name { get; set; }
        public string ShapeFile { get; set; }

        public virtual ICollection<Scenario> Scenarios { get; set; }

        public string ProjectId { get; set; }
        [JsonIgnore]
        public virtual Project Project { get; set; }
    }
}

如 cmets 中所述,更多信息可在 http://blogs.msdn.com/b/azuremobile/archive/2014/05/27/how-to-expand-linked-entities-from-mobileservices-client-and-mobileservices-service.aspx 找到。

【讨论】:

    猜你喜欢
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    相关资源
    最近更新 更多