-
Notifications
You must be signed in to change notification settings - Fork 383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to create CHECK constraint Use Fluent API? #831
Comments
I successfully checked our support for this in the using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.TestUtilities;
using MySql.Data.MySqlClient;
using Pomelo.EntityFrameworkCore.MySql.FunctionalTests.TestUtilities;
using Xunit;
namespace Pomelo.EntityFrameworkCore.MySql.FunctionalTests
{
public class ReportedIssuesMySqlTest
: IClassFixture<ReportedIssuesMySqlTest.ReportedIssuesMySqlFixture>
{
[ConditionalFact]
public virtual void TestIssue831()
{
using var context = CreateContext();
context.Set<test1>()
.Add(new test1 { gender = "trans" });
var outerExpection = Assert.Throws<DbUpdateException>(() => context.SaveChanges());
var innerException = Assert.IsType<MySqlException>(outerExpection.InnerException);
Assert.Equal(
innerException.Message,
"Check constraint 'constraint_gender' is violated.");
}
public class test1
{
public int id { get; set; }
public string gender { get; set; }
}
public class ReportedIssuesMySqlFixture : SharedStoreFixtureBase<PoolableDbContext>
{
protected override void OnModelCreating(
ModelBuilder modelBuilder,
DbContext context)
{
modelBuilder.Entity<test1>(entity =>
{
entity.HasKey(e => e.id);
entity.Property(e => e.gender)
.HasMaxLength(6);
entity.HasCheckConstraint(
"constraint_gender",
"`gender` = 'male' or `gender` = 'female'");
});
}
protected override void Seed(PoolableDbContext context)
{
context.Set<test1>().AddRange(
new test1 { gender = "male" },
new test1 { gender = "female" }
);
context.SaveChanges();
}
protected override ITestStoreFactory TestStoreFactory
=> MySqlTestStoreFactory.Instance;
protected override string StoreName { get; } = "ReportedIssues";
}
public ReportedIssuesMySqlTest(ReportedIssuesMySqlFixture fixture)
=> Fixture = fixture;
protected ReportedIssuesMySqlFixture Fixture { get; }
protected DbContext CreateContext() => Fixture.CreateContext();
}
} This test throws the following exception (wrapped in a
The test generates the following SQL: CREATE TABLE `test1` (
`id` int NOT NULL AUTO_INCREMENT,
`gender` varchar(6) NULL,
CONSTRAINT `PK_test1` PRIMARY KEY (`id`),
CONSTRAINT `constraint_gender` CHECK (`gender` = 'male' or `gender` = 'female')
);
@p0='male' (Size = 6)
INSERT INTO `test1` (`gender`)
VALUES (@p0);
SELECT `id`
FROM `test1`
WHERE ROW_COUNT() = 1 AND `id` = LAST_INSERT_ID();
@p0='female' (Size = 6)
INSERT INTO `test1` (`gender`)
VALUES (@p0);
SELECT `id`
FROM `test1`
WHERE ROW_COUNT() = 1 AND `id` = LAST_INSERT_ID(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to create a check constraint,eg
gender
I don't want use
migrationBuilder.Sql()
create check constraints, so I want to known Weather support Create a CHECK constraint Using Fluent API ? same asHasCheckConstraint
...(I doesn't found).Thank you for your help
The text was updated successfully, but these errors were encountered: