POCO LinqToSQL错误:System.InvalidOperationException:错误的存储属性:

时间:2011-04-01 18:02:12

标签: linq-to-sql asp.net-mvc-3

我缺少什么?

City.cs


[Table(Name = "City")]
    public class City
    {
        [Column(IsDbGenerated = true, IsPrimaryKey = true)]
        public int CityId { get; set; }

        [Column(CanBeNull = false)]
        public string CityName { get; set; }


        private EntitySet<CityLocality> _cityLocalities;

        [Association(Storage = "_CityLocality", OtherKey = "CityId", Name = "City_CityLocality")]
        public EntitySet<CityLocality> CitiyLocalities
        {
            get { return _cityLocalities; }
            set { _cityLocalities.Assign(value);}
        }

CityLocality

  [Table(Name = "CityLocality")]
    public class CityLocality
    {
       [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int LocalityId { get; set; }

        [Column(CanBeNull = false)]
        public string LocalityName { get; set; }

        [Association(Name = "FK_CityLocation_City", Storage = "_City", ThisKey = "CityId", OtherKey = "CityId", IsForeignKey = true)]
        [Column(CanBeNull = false)]
        public int CityId { get; set; }


    }

SqlCityRepository


 public class SqlCityRepository : ICityRepository
    {
        private Table<City> _citys;

        public SqlCityRepository(string connectionString)
        {
            _citys = (new DataContext(connectionString)).GetTable<City>();

        }

        public IQueryable<City> Cities
        {
            get { return _citys; }
        }

        public void Add(City city)
        {
            if(city.CityId == 0)
            {
                _citys.InsertOnSubmit(city);
            }
            else
            {
                _citys.Attach(city);
                _citys.Context.Refresh(RefreshMode.KeepCurrentValues,city);
            }
            _citys.Context.SubmitChanges();
        }

        public City GetById(int cityId)
        {
            return (from c in _citys where c.CityId == cityId select c).FirstOrDefault();
        }
    }

的TestFixture


   [Test]
        public void CityAddFunctionalityShouldWork()
        {
            var repository = new SqlCityRepository(_connectionSting);
            var city = new City();
            city.CityName = "BagChenDil";


            using(var scope = new TransactionScope())
            {
                repository.Add(city);
                var banChenDil = (from c in repository.Cities select c).FirstOrDefault();

                Assert.AreEqual(banChenDil.CityName, city.CityName);

                scope.Dispose();
            }

        }

错误讯息:

System.InvalidOperationException:Bad Storage属性:成员'Entities.Location.City.CitiyLocalities'上的'_CityLocality'。

1 个答案:

答案 0 :(得分:0)

这有几个问题。

1)通知。

private EntitySet<CityLocality> _cityLocalities;

这应该是。

private EntitySet<CityLocality> _cityLocalities = new EntitySet<CityLocality>();

2)[Association(Storage = "_CityLocality", 这应该是。

   [Association(Storage = "_cityLocalities",

3)检查了DB Schema,发现有列可以为空,以防我将它们从可空变为可空。

事情很顺利。

亲切的问候 维奈。