When using Dapper with Entity Framework, should table constraints be relaxed or disabled?

.net-4.0 dapper edmx entity-framework

Question

I would like to set a do something like this dataSet1.EnforceConstraints = false; using the Dapper.Net ORM and Entity Framework.

I created the models in VS2010 where a property:

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String ethnicname
    {
        get
        {
            return _ethnicname;
        }
        set
        {
            OnethnicnameChanging(value);
            ReportPropertyChanging("ethnicname");
            _ethnicname = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("ethnicname");
            OnethnicnameChanged();
        }
    }

... when value is null, I get a constraint exception at StructuralObject.SetValidValue... . How and where do I turn this off?

My data call looks like this:

    public DAL.Models.PROFILE GetProfile(int profileID)
    {
        using (IDbConnection connection = OpenConnection("MyDBConnectionString"))
        {
            try
            {

                var profiles = connection.Query<DAL.Models.PROFILE>("SELECT * FROM PROFILES WHERE ID=@ID", new { ID = profileID }); // IEnumerable
                var profile = profiles.First<DAL.Models.PROFILE>();

                return profile;
            }
            catch (Exception ex)
            {
                ErrorLogging.Instance.Fatal(ex);
                return null;
            }
        }
    }
1
0
5/25/2012 1:22:37 PM

Accepted Answer

If you are using autogenerated EntityObject based entities and you mark field as not nullable (your database has it not nullable) you cannot turn this validation off. The second parameter of SetValidValue decides if null is allowed or not.

1
5/25/2012 10:02:15 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow