Pomelo.EntityFrameworkCore.MySql: SQL Syntax Error
Environment:
MySQL version: 5.7.18 Operating system: Win7x64;Win10x64;debian8x64 Pomelo.EntityFrameworkCore.MySql version: 1.1.2 NetStandard 1.4
DbContext::OnModelCreating:
modelBuilder.Entity<DateValueEntity>(buildAction => {
buildAction.HasKey(dataValueEntity => dataValueEntity.Id);
buildAction.HasOne(dataValueEntity => dataValueEntity.SensorEntity)
.WithMany(sensor => sensor.DataValues)
.HasForeignKey(dataValueEntity => dataValueEntity.SensorEntityId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<SensorEntity>(buildAction => {
buildAction.HasKey(sensor => sensor.Id);
buildAction.HasOne(sensor => sensor.Device)
.WithMany(device => device.Sensors)
.HasForeignKey(sensor => sensor.DeviceId)
.OnDelete(DeleteBehavior.Cascade);
buildAction.HasMany(sensor => sensor.DataValues)
.WithOne(dataValueEntity => dataValueEntity.SensorEntity)
.HasForeignKey(dataValueEntity => dataValueEntity.SensorEntityId);
});
DateValueModel:
[Table(name: nameof(DateValueEntity))]
public class DateValueEntity : IDateValue{
[ForeignKey(nameof(DateValueEntity) + "_" + nameof(Database.SensorEntity)+"_ForeignKey")]
[Required]
public int? SensorEntityId { get; set; }
public virtual SensorEntity SensorEntity { get; set; }
[Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public DateTime TimeStamp { get; set; }
[Required]
public float Value { get; set; }
}
SensorEntity:
[Table(name: nameof(SensorEntity))]
public class SensorEntity : ISensor<DateValueEntity> {
public virtual int? DeviceId { get; set; }
public virtual DeviceEntity Device { get; set; }
[Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public SensorName Name { get; set; }
[Required]
public SensorGranularity Granularity { get; set; }
[Required]
public SensorType Type { get; set; }
[Required]
public UnitType Unit { get; set; }
public virtual FeedConnectorEntity FeedConnector { get; set; }
public virtual ICollection<DateValueEntity> DataValues { get; set; } = new Collection<DateValueEntity>();
//Sync this enum with NobelGrid.Frontend/wwwroot/api_client/Sensor.ts
public enum SensorName{
L1 = 1,
L2 = 2,
L3 = 3,
NONE = 4,
TOTAL=5
}
//Sync this enum with NobelGrid.Frontend/wwwroot/api_client/Sensor.ts
public enum SensorGranularity{
INSTANT_UPDATE = 60,
HOURLY_UPDATE = 60 * 60,
DAILY_UPDATE = 60 * 60 * 24,
QUARTERLY_UPDATE = 60 * 15,
}
//Sync this enum with NobelGrid.Frontend/wwwroot/api_client/Sensor.ts
public enum SensorType {
ACTIVE_POWER = 0,
REACTIVE_POWER = 1,
POWER_FACTOR = 2,
VOLTAGE = 3,
FREQUENCY = 4,
CO2_EMISSIONS = 5,
STATE_OF_CHARGE = 6,
STATUS = 7,
SETPOINT = 8,
TEMPERATURE = 9,
HUMIDITY = 10,
LUMINOSITY = 11,
ENERGY_COST = 12,
FAILURE_COUNTER = 13,
ACTIVE_ENERGY = 14,
REACTIVE_ENERGY = 15,
DIMMING_FACTOR = 16
}
//Sync this enum with NobelGrid.Frontend/wwwroot/api_client/Sensor.ts
public enum UnitType{
WATT = 0,
NULL = 1,
KWH = 2,
HZ = 3,
KVArH = 4,
VAr = 5,
V = 6,
MONETARY_UNIT = 7, //can be pounds or euros
GRAMS_CO2 = 8,
PERCENTAGE = 9,
LUX = 10,
TEMPERATURE_UNIT = 11,//na be degrees or farenheit
BOOLEAN = 12,
FACTOR=13
}
}
Test that pass OK:
[Fact]
public void AddTest() {
var testInstance = DatabaseTestsUtils.DataValueEntityTestingInstances.SimpleDateValueEntity;
testInstance.SensorEntityId = this.SensorEntityInstance.Id;
this.DbContext.DateValueEntitySet.Add(testInstance);
this.DbContext.SaveChanges();
}
Test that NOT pass OK:
[Fact]
public void AddTest() {
var testInstance = DatabaseTestsUtils.SensorEntityTestingInstances.ComplexSensorEntity; //This is a Sensor with the DateValues filled with multiple items
testInstance.DeviceId = this.SensorEntityInstance.Id;
this.DbContext.SensorEntitySet.Add(testInstance);
this.DbContext.SaveChanges();
}
Exception Message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT VALUES;
SELECT LAST_INSERT_ID()' at line 2
Stack Trace:
at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet)
at MySql.Data.MySqlClient.MySqlDataReader.<ReadFirstResultSetAsync>d__62.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MySql.Data.MySqlClient.MySqlDataReader.<CreateAsync>d__61.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MySql.Data.MySqlClient.CommandExecutors.TextCommandExecutor.<ExecuteReaderAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at Microsoft.EntityFrameworkCore.Storage.Internal.MySqlRelationalCommand.<ExecuteAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Storage.Internal.MySqlRelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Update.Internal.MySqlModificationCommandBatch.Execute(IRelationalConnection connection)
Aditional Information:
With https://github.com/SapientGuardian/SapientGuardian.EntityFrameworkCore.MySql and EntityFrameworkCore.SQLite this error don’t extists.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 19 (5 by maintainers)
@rasert I have the same problem with a tabla where it have only the ID field, and I fixed with a workaround. I added a dummy field with a default value: