2

EntityFramework4.3.1.0を使用しています

SQL Server 2008 Express

ビューがあります

SELECT  dbo.Dealers.Name AS DealerName, 
        dbo.Dealers.LogoImage, 
        dbo.DealersProducts.Price, 
        dbo.DealersProducts.StatusType, 
        dbo.Products.Description,                       
        dbo.Products.Name, 
        dbo.DealersProducts.DealerId, 
        dbo.Products.Id 
FROM dbo.Dealers 
INNER JOIN
dbo.DealersProducts 
ON dbo.Dealers.Id = dbo.DealersProducts.DealerId 
INNER JOIN                       
dbo.Products 
ON dbo.DealersProducts.ProductId = dbo.Products.Id

そして私はエンティティを持っています

public class DealerViews : BasePersistentEntity
{
    public int DealerId { get; set; }

    public string DealerName { get; set; }

    public string LogoImage { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public decimal Price { get; set; }

    public int StatusType { get; set; }
}

私が返品ディーラーに使用するこのビューは、リクエスト商品を持っています。SQL ManagerでSQLクエリを要求すると、正しい結果がゲートされますが、EntityFrameworkでは奇妙な結果が得られます。例

SQLの結果

ディーラー1

ディーラー2

EntityFrameworkの結果

ディーラー1

ディーラー1

コントローラのコード

public ActionResult ShowProductDealers(int id)
{
    var dealer = this.dealerService.GetDealerByProductId(id);

    return this.PartialView("ShowProductDealers", dealer);
}

稼働中のコード

public IEnumerable<DealerViews> GetDealerByProductId(int id)
{
    return this.repositoryViews.All.Where(item => item.Id == id);
}

リポジトリ内のコード

public class SGNRepository<T> where T : BasePersistentEntity
{
    public readonly SGNContext<T> Context = new SGNContext<T>();

    public IQueryable<T> All
    {
        get { return this.Context.Table; }
    }

    public IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = this.Context.Table;
        return includeProperties.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
    }

    public T Find(int id)
    {
        return this.Context.Table.Find(id);
    }

    public void InsertOrUpdate(T item)
    {
        if (item.Id == default(int))
        {
            // New entity
            this.Context.Table.Add(item);
        }
        else
        {
            // Existing entity
            this.Context.Entry(item).State = EntityState.Modified;
        }

        this.Save();
    }

    public void Delete(int id)
    {
        var item = this.Context.Table.Find(id);
        this.Context.Table.Remove(item);
        this.Save();
    }

    private void Save()
    {
        this.Context.SaveChanges();
    }
4

1 に答える 1

5

マッピングに問題があります。エンティティの主キーとしてどの列を選択しましたか?この場合Product.Id、主キーとしてあなたが持っていて、この製品には多くのディーラーがいると思います。

EFは、一意のIDとして主キーを使用します。各レコードは一意に識別される必要があります。EFがレコードをロードし、内部IDマップに一意のIDがない場合、EFはそのレコードからエンティティインスタンスを作成し、IDマップに配置します。次回、同じ一意のIDを持つレコードを取得するときは、新しいエンティティインスタンスを作成せず、代わりにIDマップに格納されているインスタンスを使用します。それがあなたが同じ結果を得る理由です。

ところで。ビューから作成されたエンティティを挿入、削除、または更新できないため(これらすべての操作に対してストアドプロシージャまたはカスタムSQLコマンドをマップしない限り)、リポジトリが間違っているため、そのようなメソッドを公開しないでください。

もう1つのポイントは、単一の呼び出しだけをラップするときにリポジトリとサービスがあるのはなぜですか=付加価値がなく、特にリポジトリが1つのエンティティタイプで単純なCRUD操作のみをサポートしている場合はそうする理由がありません。

于 2012-05-26T14:19:33.623 に答える