数据库优先键查找表和表

时间:2019-04-13 00:27:00

标签: entity-framework-6 asp.net-core-2.1

我在Entity Framework上使用数据库优先方法。 我有3节课: 1)类别:查找表。 2)商店:拥有类别属性。 3)SystemUser:与商店之间有关系

我想将存储添加到具有Category属性的数据库,但是出现错误

“字符串或二进制数据将被截断。 该语句已终止。”

 public class SystemUser
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual List<Store> Stores { get; set; }

        public SystemUser()
        {
            this.Stores = new List<Store>();
        }

    }

     public class Category
        {

            public int Id { get; set; }
            public string Name { get; set; }

            public List<Store> Stores { get; set; }

            public Category()
            {
                this.Stores = new List<Store>();
            }



    public class Store
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string CountryCode { get; set; }
            public string Email { get; set; }


            public virtual Category Category { get; set; }

             // other relation
            public virtual SystemUser User { get; set; }
             // other relation
            public virtual StoreStockDetail StoreStockDetail { get; set; }

        }

    public void Process(){

                  var user = await _context.SystemUser.Include(x => 
               x.Stores).FirstOrDefaultAsync(x => x.UserName == userAddStore.UserName);


                var category = await _context.Category.FirstOrDefaultAsync(x => (int)x.Id == userAddStore.Category);


                // Map to Store
                Store store = new Store()
                {
                    Category = category,
                    CountryCode = userAddStore.CountryCode,
                    Email = userAddStore.Email,
                    Name = userAddStore.Name
                };


                //store.StoreStockDetail = storeStockDetail;
                user.Stores.Add(store);
                await _context.SaveChangesAsync(); // error happens there
    }

我想保留类别属性作为查找表。 此类别应添加到商店。

0 个答案:

没有答案