簡體   English   中英

實體框架5不會更新多對多關系

[英]Entity Framework 5 doesn't update Many to Many relation

我在SQL2008數據庫中具有以下表:

住所

code    varchar(18)
name    varchar(80)

該表有更多列,但為簡單起見,在此已將其刪除。

屬性

code    int
name    varchar(50)

住宿屬性

AccommodationCode   varchar(18)
AttributeCode   int

如您所見,AccommodationAttributes描述了Accommodations和Attributes之間的多對多關系。

我首先使用數據庫創建了模型(EF5),並且創建了兩個與導航屬性鏈接的類。

所有這些似乎都是正確的。

我想做的是在db中添加值,但是盡管我能夠添加Accommodations和Attributes,但似乎無法使其在AccommodationAttributes表中添加相應的值。

我正在從XML文件讀取。

編輯

以下是我正使用的代碼:

public static void UpdateAccommodation(string file)
{
    InterHomeEntities ih = new InterHomeEntities();
    Stopwatch sw = new Stopwatch();
    ih.Configuration.AutoDetectChangesEnabled = false;
    ih.Configuration.ValidateOnSaveEnabled = false;
    ih.Configuration.LazyLoadingEnabled = false;

    XElement xe = XElement.Load(file);
    DateTime DayToProcess = DateTime.Now.AddDays(Properties.Settings.Default.InterHome_DaysToProcess);

    var Attributes = xe.XPathSelectElements("//attribute").Select(x => x.Value).Distinct();
    foreach (var attribute in Attributes)
    {
        Attribute at = ih.Attributes.Where(x => x.name == attribute).SingleOrDefault();
        bool newEntry = at == null ? true : false;
        at = newEntry ? new Attribute { name = attribute } : at;
        ih.Attributes.Attach(at);
        ih.Entry(at).State = newEntry ? System.Data.EntityState.Added : System.Data.EntityState.Modified;
        ih.SaveChanges();
    }
    var Accommodations = from c in xe.Elements("accommodation") select c;
    int AccomodationCount = Accommodations.Count();
    int AccomodationIndex = 0;
    foreach (var accommodation in Accommodations)
    {
        AccomodationIndex++;
        var AccCode = accommodation.Element("code").Value;
            try
            {
                Accommodation a = ih.Accommodations.Where(x=>x.code == AccCode).SingleOrDefault();
                bool newAccommodation = a == null ? true : false;
                a = !newAccommodation ? a :
                    new Accommodation
                    {
                        code = accommodation.Element("code") == null ? null : accommodation.Element("code").Value,
                        name = accommodation.Element("name") == null ? null : accommodation.Element("name").Value,
                        country = accommodation.Element("country") == null ? null : accommodation.Element("country").Value,
                        region = accommodation.Element("region") == null ? null : accommodation.Element("region").Value,
                        place = accommodation.Element("place") == null ? null : accommodation.Element("place").Value,
                        zip = accommodation.Element("zip") == null ? null : accommodation.Element("zip").Value,
                        type = accommodation.Element("type") == null ? null : accommodation.Element("type").Value,
                        quality = accommodation.Element("quality") == null ? (byte?)null : Convert.ToByte(accommodation.Element("quality").Value),
                        details = accommodation.Element("details") == null ? null : accommodation.Element("details").Value,
                        brand = accommodation.Element("brand") == null ? null : accommodation.Element("brand").Value,
                        pax = accommodation.Element("pax") == null ? (double?)null : Convert.ToDouble(accommodation.Element("pax").Value),
                        sqm = accommodation.Element("sqm") == null ? (double?)null : Convert.ToDouble(accommodation.Element("sqm").Value),
                        floor = accommodation.Element("floor") == null ? (double?)null : Convert.ToDouble(accommodation.Element("floor").Value),
                        rooms = accommodation.Element("rooms") == null ? (double?)null : Convert.ToDouble(accommodation.Element("rooms").Value),
                        bedrooms = accommodation.Element("bedrooms") == null ? (double?)null : Convert.ToDouble(accommodation.Element("bedrooms").Value),
                        toilets = accommodation.Element("toilets") == null ? (double?)null : Convert.ToDouble(accommodation.Element("toilets").Value),
                        bathrooms = accommodation.Element("bathrooms") == null ? (double?)null : Convert.ToDouble(accommodation.Element("bathrooms").Value),
                        lat = accommodation.Element("geodata") == null || accommodation.Element("geodata").Element("lat") == null ? null : accommodation.Element("geodata").Element("lat").Value,
                        lng = accommodation.Element("geodata") == null || accommodation.Element("geodata").Element("lng") == null ? null : accommodation.Element("geodata").Element("lng").Value,
                        LastUpdated = DateTime.Now
                    };
                foreach (var attribute in accommodation.Elements("attributes").Elements("attribute").Select(x=>x.Value))
                {
                    Attribute at = ih.Attributes.Where(x => x.name == attribute).SingleOrDefault();
                    a.Attributes.Add(at);
                }
                if (newAccommodation)
                {
                    ih.Accommodations.Add(a);
                }
                else
                {
                    ih.Entry(ih.Accommodations.Where(x => x.code == a.code).SingleOrDefault()).CurrentValues.SetValues(a);
                    ih.Entry(ih.Accommodations.Where(x => x.code == a.code).SingleOrDefault()).State = System.Data.EntityState.Modified;
                }

                ih.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
    ih.SaveChanges();
}

運行此代碼后,我在SQL中運行以下命令:

select COUNT(*) from Accommodations
select COUNT(*)from Attributes
select COUNT(*)from AccommodationAttributes

但是,盡管我在兩個表中看到了條目,但鏈接表附帶了0行。

我嘗試了其他變體,例如將對象附加到上下文,或隱式指定它是已修改的對象。

到該代碼運行時,我可以確定屬性已插入數據庫中,但可容納性是插入或更新。

更新

經過進一步調查,似乎當我添加新的Accommodation時它可以工作,但是當Accommodation已在數據庫中並且我只是添加新屬性時,它就失敗了。 在開發過程中,我首先添加了Accommodation,然后在開發的后續步驟中創建了導入屬性的過程。 因此,當適應和屬性都已經在數據庫中時,我需要找到一種更新關系的方法。 我渴望聽到您的想法,

詹尼斯

確保設置以下內容:

在表Accomodation PK是代碼。

表Attrrrribute PK是代碼。

在表AccomodationAtrribute PK中,AccomodationCode + AttributeCode。

在表AccomodationAttribute中,將AccomondationCode的前一個鍵設置為表Accomodation中的列代碼。

在表AccomodationAttribute中,將AttributeCode的前鍵設置為表Attribute中的列代碼。

同樣,要填充鏈接表,您需要將屬性實例鏈接到代碼中的住宿,反之亦然。 像這樣

accomodation.Attrbutes.Add(attribute);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM