Relax or disable table constraints when using Dapper and Entity Framework?

.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;
            }
        }
    }

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.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why