English 中文(简体)
Linq GetTable<> 返回空行
原标题:Linq GetTable< > return empty rows

我有一个非常奇怪的问题。 我接着是MVC的辅导。

我有一个接口:

namespace DomainModel.Abstract
{
    public interface IProductsRepository
    {
        IQueryable<Product> Products { get; }
    }
}

存放处继承:

namespace DomainModel.Concrete
{
    public class SqlProductsRepository : IProductsRepository
    {
        private Table<Product> productsTable;
        public SqlProductsRepository(string connectionString)
        {
            try
            {
                productsTable = (new DataContext(connectionString)).GetTable<Product>();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
        }
        public IQueryable<Product> Products
        {
            get { return productsTable; }
        }
    }
}

我的产品阶级喜欢这样做:

namespace DomainModel.Entities
{
    [Table(Name = "Products")]
    public class Product
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int ProductID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Category { get; set; }       
        public decimal Price { get; set; }

    }
}

我的控制者也想到这一点:

namespace WebUI.Controllers
{
    public class ProductsController : Controller
    {

        private IProductsRepository productsRepository;
        public ProductsController()
        {
            string connString = @"Data Source=INFO_KW-HZ7YX91;Initial Catalog=SportStore;Integrated Security=True";
            productsRepository = new SqlProductsRepository(connString);

        }
        public ViewResult List()
        {
            return View(productsRepository.Products.ToList());

        }

    }
}

我的产品表有8行。 当我管理网站时,该网站有8个浏览点,但所有浏览点都是零。 当我gged倒时,我可以看到,有8个产品可用,有正确的栏目,但插图数据无效,分类数据为零。

我有什么错误?

增 编

问题回答

从上次我做过习惯属性一样,这已经相当好了,但我猜测,你需要为你们需要坚持的所有财产制定<条码>。 类似:

    [Column]
    public string Name { get; set; }
    [Column]
    public string Description { get; set; }
    [Column]
    public string Category { get; set; }       
    [Column]
    public decimal Price { get; set; }

假设贵方位表的实地名称与财产名称相应。

你在任何一栏中加上[哥伦]属性,但你的PK除外。 Linq ToSql非常称重,手提编码将充满风险,(很可能)令人恐惧的例外。 你们应该使用 s。 挖掘或自行制造。





相关问题
LINQ to SQL as databinding source for WPF Treeview

I wonder if someone could provide a simple example of the following. (preferably in VB.Net): I have an SQL database with related tables and I am successfully using LINQ to SQL in other areas of my ...

Linq to SQL insert/select foreign/primary records

I have table A and Table B. Table B contains two columns, Name and ID. Table A contains several columns and a foreign key column pointing to B.ID called B_ID On the client side, I have all the data I ...

LINQ to SQL Dynamic Sort question

How do I code the Select clause in my LINQ satament to select column aliases so I can sort on them basically I want to accomplish this SQL statement in LINQ: select type_id as id, ...

Using a schema other than dbo in LinqToSql Designer

Is there a way to specify in a data connection or the LinqToSql Designer a schema? Whenever I go to setup a data connection for LinqToSql there doesn t seem to be anyway to specify a schema and I ...

热门标签