English 中文(简体)
带有 View 的实体框架错误吗?
原标题:Entity Framework bug with Views?

I使用实体框架4.3.1.0

SQL 2008 SQL 服务器

我有意见

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

This View I use for return dealer ho have request product. When I request SQL query in SQL Manager I gate correct result but in Entity Framework I get strange result. Examples

<强 > SQL 结果

发牌者 1

发牌者 2

<强度 > 实体框架结果

发牌者 1

发牌者 1

控制器 < / 强 > 中的 < 强 > 代码

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

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

Code in service

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();
    }
最佳回答

您的映射有问题。 您选择哪个列作为实体的主要密钥? 我期待您在此情况下将 < code> production. Id 作为主密钥, 而该产品有许多经销商 。

EF 使用主键作为独有身份识别。 每个记录都必须被单独识别。 如果 EF 装入记录, 并且没有其 < a href=' https://stackoverflow.com/ questions/365309//entity-framework- and-connection-pooling 3653392", 则在内部身份地图 < /a> 中, 它从该记录中创建和实体实例, 并将其放入身份地图。 下次它用相同的独特身份识别记录不会创建新实体实例, 而是在身份地图中存储实例 。 这就是为什么你得到相同结果的原因 。

Btw. 您的仓库是错误的, 因为您无法插入、 删除或更新从视图创建的实体( 除非您映射存储程序或为所有这些操作自定义 SQL 命令), 因此它不应该暴露这些方法 。

另一点是,为什么您有存储处和服务,当它只包装一个电话时,它就等于没有增加值,也没有这样做的理由,特别是当您的存储处只支持一个实体类型的简单的 CROUD 操作时。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签