簡體   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