保存时,EntityFramework使FK关系为空

时间:2017-11-16 12:09:16

标签: c# entity-framework entity-framework-6

在EF6中,我从数据库加载我的机场:

var existingAirport = db.Airports.SingleOrDefault(a => a.ID == airportID)
//airport is loaded, it has a countryID and virtual Country object

我执行一些逻辑,进行一些修改以改变关系,改变国家对象和FK ID:

//it's a bit more complex, but simplified:
existingAirport.Country = //the someOtherCountryObject
existingAirport.CountyID = //the someOtherCountryObject.ID

我保存更改(应该将机场分配给新国家/地区)

db.SaveChanges();

问题是EF决定使DB和内存中的关系为空(existingAirport.Country和existingAirport.CountyID现在都为null)。机场现在与 新旧国家相关联。

有人能想到EF这样做的原因吗?新的FK指向一个有效的国家。

2 个答案:

答案 0 :(得分:2)

看起来您正在尝试添加新的国家/地区,但我没有看到您的新国家/地区实体已添加到上下文中。

请参阅这些帖子以获得一个想法。 Updating child objects in Entity Framework 6Entity Framework 6: Adding child object to parent's list vs. setting child's navigation property to parent

答案 1 :(得分:0)

我花了一段时间,但这是由我模型其他部分的循环引用引起的。例如,我持有一个国家/地区列表,其中一个国家/地区对象(在这种情况下为 someOtherCountryObject )具有EF生成的对机场的引用。

我通过在映射阶段忽略这些引用来解决问题,即在将国家/地区映射到数据库时:

tinymce.init({
            selector: '#title, #thumbnail_title',
            height: 100,
            theme: 'modern',
            menubar: 'edit insert format tools',

            plugins: 'searchreplace autolink directionality image link media advlist textcolor wordcount imagetools contextmenu colorpicker textpattern code',
            toolbar1: 'formatselect fontselect fontsizeselect | bold italic strikethrough forecolor backcolor | alignleft aligncenter alignright alignjustify  | numlist bullist outdent indent  | removeformat code',
            image_advtab: true,
            image_dimensions: false,
            image_class_list: [
                {title: 'responsive', value: 'img-responsive'},
            ],
            content_css: [
                '//fonts.googleapis.com/css?family=Amaranth|Anton|Dancing+Script|Didact+Gothic|Fira+Sans+Condensed|Francois+One|Lato|Lobster|Lobster+Two|Muli|Open+Sans|Pacifico|Passion+One|Playfair+Display|Playfair+Display+SC|Quattrocento|Quattrocento+Sans|Sanchez|Shadows+Into+Light|Shadows+Into+Light+Two|Spectral+SC|Yrsa',

            ],
            font_formats : 'Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats;Didact Gothic=Didact Gothic, sans-serif;Open Sans=Open Sans, sans-serif;Lato=Lato, sans-serif;Spectral SC=Spectral SC, serif;Playfair Display SC=Playfair Display SC, serif;Muli=Muli, sans-serif;Anton=Anton, sans-serif;Lobster=Lobster, cursive;Lobster Two=Lobster Two, cursive;Playfair Display=Playfair Display, serif;Pacifico=Pacifico, cursive;Shadows Into Light=Shadows Into Light, cursive;Shadows Into Light Two=Shadows Into Light Two, cursive;Dancing Script=Dancing Script, cursive;Francois One=Francois One, sans-serif;Passion One=Passion One, cursive;Sanchez=Sanchez, serif;Quattrocento Sans=Quattrocento Sans, sans-serif;Quattrocento=Quattrocento, serif;Amaranth=Amaranth, sans-serif;Fira Sans Condensed=Fira Sans Condensed, sans-serif;Yrsa=Yrsa, serif;',

            fontsize_formats: '8px 10px 12px 14px 18px 24px 36px 42px 72px',
            protect: [
                /\<\/?(if|endif)\>/g,  // Protect <if> & </endif>
                /\<xsl\:[^>]+\>/g,  // Protect <xsl:...>
                /<\?php.*?\?>/g  // Protect php code
            ],
            forced_root_block: false,
            verify_html: true,
            branding:false,
            theme_advanced_fonts : ''
        });

这解决了这个问题,防止EF在调用SaveChanges()时使关系无效。我的Aiport现在拥有应该拥有的国家,并且没有发生重复或任何副作用。