Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在LINQ to SQL中手动设置外键关系_Asp.net_Asp.net Mvc_Linq_Linq To Sql_Asp.net Mvc 2 - Fatal编程技术网

Asp.net 如何在LINQ to SQL中手动设置外键关系

Asp.net 如何在LINQ to SQL中手动设置外键关系,asp.net,asp.net-mvc,linq,linq-to-sql,asp.net-mvc-2,Asp.net,Asp.net Mvc,Linq,Linq To Sql,Asp.net Mvc 2,我一直在阅读Steven Sanderson的《Pro ASP.NET MVC 2框架》一书。到目前为止,它一直是物候性的。。。就在我认为我知道了相当多的时候,我找到了一本书,它向我展示了我知道的是多么少 我所知甚少的一件事是如何使用LINQtoSQL。在史蒂文的书中,第4-6章创造了一个非常漂亮的小购物车。我读完了教程,一切都正常了。现在我想修改购物车以使用一个Category表,而不是将Category名称作为varchar存储在Product表中 下面是Product表对象,我将Categ

我一直在阅读Steven Sanderson的《Pro ASP.NET MVC 2框架》一书。到目前为止,它一直是物候性的。。。就在我认为我知道了相当多的时候,我找到了一本书,它向我展示了我知道的是多么少

我所知甚少的一件事是如何使用LINQtoSQL。在史蒂文的书中,第4-6章创造了一个非常漂亮的小购物车。我读完了教程,一切都正常了。现在我想修改购物车以使用一个Category表,而不是将Category名称作为varchar存储在Product表中

下面是Product表对象,我将CategoryID更改为与Categories表的外键关系

    [Table(Name="Products")]
    public class Product
    {
        [HiddenInput(DisplayValue=false)]
        [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
        public int ProductID { get; set; }

        [Required(ErrorMessage="Please enter a product name")]
        [Column] public string Name { get; set; }

        [Required(ErrorMessage="Please enter a description")]
        [DataType(DataType.MultilineText)]
        [Column] public string Description { get; set; }

        [Required]
        [Range(0.01, double.MaxValue, ErrorMessage="Please enter a positive price")]
        [Column] public decimal Price { get; set; }


        [Required(ErrorMessage="Please enter a category")]
        [Column] public int CategoryID { get; set; }

        internal EntityRef<Category> _category;
        [System.Data.Linq.Mapping.Association(ThisKey = "CategoryID", Storage = "_category")]
        public Category Category {
            get { return _category.Entity; }
            internal set { _category.Entity = value; CategoryID = value.CategoryID; }
        }


        [Column] public byte[] ImageData { get; set; }

        [ScaffoldColumn(false)]
        [Column] public string ImageMimeType { get; set; }
当我试图构建此代码时,我遇到了一个我不理解的错误:

可访问性不一致:属性类型“SportsStore.Domain.Entities.Category” 比属性“SportsStore.Domain.Entities.Product.Category”更难访问

这意味着什么/我将如何解决这个问题?

您的类“Categroy”比“Product”更不明显。“产品”具有公共属性“类别”,即公共属性。这就是“不一致的可访问性”


你必须像“产品”一样公开你的类“类别”

doh!有时候我希望我能收回一个问题。谢谢你的帮助。
    [Table(Name="Categories")]
    class Category
    {
        [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
        internal int CategoryID { get; set; }

        [Column]
        public int ParentCategoryID { get; set; }

        [Column]
        [Required]
        public string Name { get; set; }
    }