繁体   English   中英

如何将关系数据从SQL-Server绑定到具有一对多关系的实体

[英]How to bind relational data from SQL-Server to an Entity with one-to-many relationship

我正在使用带有.net核心的entity-framework6。 我正在进行一些复杂的计算以从数据库中获取一些结果,因此我在数据库中创建了一个存储过程,它给出了属性列表。 我已经成功地从数据库结果将数据绑定到Property Object。 如下所示

存储过程

ALTER PROCEDURE [dbo].[PublicPropertySearch] 
    @Latitude float,
    @Longitude float,
    @Radius float = NULL,
    @HavePets bit = NULL,
    @FullBathroomCount int = NULL,
    @BedroomCount int = NULL,
    @RentPriceFrom int = NULL,
    @RentPriceTo int = NULL,
    @PropertyTypeId int = NULL

AS
BEGIN

declare @Geography geography = GEOGRAPHY::Point(@Latitude , @Longitude , 4326);

    SET NOCOUNT ON;
    select top 50 * from dbo.Property p
    --inner join dbo.PropertyFile f on f.PropertyId = p.PropertyId and f.IsPhoto = 1
    where GeoLogic.STDistance(@Geography)/1609.344 < @Radius
    and (@HavePets is null or p.HavePets = @HavePets)
    and (@FullBathroomCount is null or p.FullBathroomCount = @FullBathroomCount)
    and (@BedroomCount is null or p.BedroomCount = @BedroomCount)
    and (@RentPriceFrom is null or p.ExpectedRentalPrice > @RentPriceFrom)
    and (@RentPriceTo is null or p.ExpectedRentalPrice < @RentPriceTo)
    and (@PropertyTypeId is null or p.PropertyTypeId = @PropertyTypeId)

    --EXEC PublicPropertySearch @Latitude=28.609255,@Longitude=-81.250135,@Radius=5

END

用于将结果与实体绑定的代码

var Properties = db.Database.SqlQuery<PublicPropertySearchResult>("exec PublicPropertySearch @Latitude, @Longitude, @Radius, @HavePets, @FullBathroomCount, @BedroomCount, @RentPriceFrom, @RentPriceTo, @PropertyTypeId", sp.ToArray()).ToList();

这是我的房产模型

    class PublicPropertySearchResult
    {
        public int PropertyId { get; set; }
        public string Title { get; set; }
        public string StreetAddress { get; set; }
        public string Description { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public int BedroomCount { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public string State { get; set; }
        public double ExpectedRentalPrice { get; set; }
        public string Country { get; set; }
        public string Area { get; set; }
        public string AreaUnit { get; set; }
        public int FullBathroomCount { get; set; }
        public string LandlordCurrency { get; set; }
        public string ZipCode { get; set; }
        public int PropertyTypeId { get; set; }
    }

这是我的存储过程的输出

PropertyId  Title   Description Address StreetAddress   City    State   Country ZipCode Latitude    Longitude   HideStreetAddress   PropertyTypeId  ListingTypeId   Area    AreaUnit    BedroomCount    FullBathroomCount   HalfBathroomCount   Laundry IsFurnished HasParking  SitePaymentEnabled  TotalTenantsLiving  ExpectedRentalPrice HavePets                                                                    
163 3 Bed With Parking Features:(Assigned,Boat,Covered) Flooring: (Carpet,Ceramic Tile)     NULL    NULL    CASSELBERRY NULL    US  32707   28.636128   -81.320118  1   1   6   2811    NULL    3   3   0   Upper Level 0   1   NULL    NULL    220000  1                                                                       
234 4 Bed With Parking Features:(Other) Flooring: (Porcelain Tile)      NULL    NULL    WINTER PARK NULL    US  32789   28.599719   -81.331453  1   2   6   4715    NULL    4   5   1       0   1   NULL    NULL    1190900 0                                                                                                               
448 3 Bed  Flooring: (Terrazzo)     NULL    NULL    ORLANDO NULL    US  32817   28.609255   -81.250135  1   2   6   2016    NULL    3   2   0   Inside,Laundry Closet   0   0   NULL    NULL    415000  1                                       
515 3 Bed  Flooring: (Carpet,Ceramic Tile)      NULL    NULL    MAITLAND    NULL    US  32751   28.630359   -81.320365  1   3   6   NULL    NULL    3   2   1   Laundry Closet,Upper Level  0   0   NULL    NULL    269900  1

从这段代码我只从属性表获取属性对象现在我还想从数据库中的PropertyFiles表绑定Files对象(在数据库中的PropertyFilesProperty表之间存在关系)。

我的问题是数据库需要什么类型的输出才能绑定PropertyFiles对象。 在下面给出的实体中

    class PublicPropertySearchResult
    {
        public int PropertyId { get; set; }
        public string Title { get; set; }
        public string StreetAddress { get; set; }
        public string Description { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public int BedroomCount { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public string State { get; set; }
        public double ExpectedRentalPrice { get; set; }
        public string Country { get; set; }
        public string Area { get; set; }
        public string AreaUnit { get; set; }
        public int FullBathroomCount { get; set; }
        public string LandlordCurrency { get; set; }
        public string ZipCode { get; set; }
        public int PropertyTypeId { get; set; }
        public virtual ICollection<PropertySearchFile> Files { get; set; }
    }

    public class PropertySearchFile
    {
        public int PropertyFileId { get; set; }
        public string FileUrl { get; set; }
        public string FileName { get; set; }
        public virtual PublicPropertySearchResult Property { get; set; }
    }

为了识别关系的“一”部分,“多”部分需要包含“一个”标识符。 通常这样做是这样的:

public class PropertySearchFile
    {
        public int PropertyFileId { get; set; }
        public string FileUrl { get; set; }
        public string FileName { get; set; }

        public int PropertyId { get; set; } //THat is assuming that PropertyId is the PK for the PublicPropertySearchResult class 
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM