Why is EF treating a char property as an actual guid?
up vote
0
down vote
favorite
What I have actually is a property of type char which I use to store the string value of a guid.
My current setup:
- MySQL
- EF Code-First
- EF Migration
Model:
public class Employee
{
public string Id { get; set; }
}
EF config
public class EmployeeConfiguration : EntityConfiguration<Employee>
{
public EmployeeConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id)
.HasColumnType(“char”)
.HasMaxLength(36)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
The problem:
System.FormatException is thrown if I do the following:
1. Insert with an Id that doesn’t follow the format of a guid.
2. Querying the above Employee class from database that contains an Id that doesn’t follow the format of a guid.
If I try to insert or query straight to the database using sql (without using EF), it’s all fine.
Can you please guide me how to work around this problem?
Edit 1:
Error message
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
Solution that worked for me
From "Jason Armstrong" suggestion, I used a different char length, I went with a char(37).
c# mysql entity-framework ef-code-first ef-migrations
add a comment |
up vote
0
down vote
favorite
What I have actually is a property of type char which I use to store the string value of a guid.
My current setup:
- MySQL
- EF Code-First
- EF Migration
Model:
public class Employee
{
public string Id { get; set; }
}
EF config
public class EmployeeConfiguration : EntityConfiguration<Employee>
{
public EmployeeConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id)
.HasColumnType(“char”)
.HasMaxLength(36)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
The problem:
System.FormatException is thrown if I do the following:
1. Insert with an Id that doesn’t follow the format of a guid.
2. Querying the above Employee class from database that contains an Id that doesn’t follow the format of a guid.
If I try to insert or query straight to the database using sql (without using EF), it’s all fine.
Can you please guide me how to work around this problem?
Edit 1:
Error message
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
Solution that worked for me
From "Jason Armstrong" suggestion, I used a different char length, I went with a char(37).
c# mysql entity-framework ef-code-first ef-migrations
1
Can you post the full stack trace? Are you sure it’s not your own code that’s throwing the exception? You say you are using this property to store a guid. Are you converting the property to/from actual guid type somewhere in your code?
– Dave M
Nov 10 at 14:29
I can’t now, I am still on my way home from work.. i will give you that tomorrow..
– devpro101
Nov 10 at 15:21
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
What I have actually is a property of type char which I use to store the string value of a guid.
My current setup:
- MySQL
- EF Code-First
- EF Migration
Model:
public class Employee
{
public string Id { get; set; }
}
EF config
public class EmployeeConfiguration : EntityConfiguration<Employee>
{
public EmployeeConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id)
.HasColumnType(“char”)
.HasMaxLength(36)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
The problem:
System.FormatException is thrown if I do the following:
1. Insert with an Id that doesn’t follow the format of a guid.
2. Querying the above Employee class from database that contains an Id that doesn’t follow the format of a guid.
If I try to insert or query straight to the database using sql (without using EF), it’s all fine.
Can you please guide me how to work around this problem?
Edit 1:
Error message
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
Solution that worked for me
From "Jason Armstrong" suggestion, I used a different char length, I went with a char(37).
c# mysql entity-framework ef-code-first ef-migrations
What I have actually is a property of type char which I use to store the string value of a guid.
My current setup:
- MySQL
- EF Code-First
- EF Migration
Model:
public class Employee
{
public string Id { get; set; }
}
EF config
public class EmployeeConfiguration : EntityConfiguration<Employee>
{
public EmployeeConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id)
.HasColumnType(“char”)
.HasMaxLength(36)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
The problem:
System.FormatException is thrown if I do the following:
1. Insert with an Id that doesn’t follow the format of a guid.
2. Querying the above Employee class from database that contains an Id that doesn’t follow the format of a guid.
If I try to insert or query straight to the database using sql (without using EF), it’s all fine.
Can you please guide me how to work around this problem?
Edit 1:
Error message
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
Solution that worked for me
From "Jason Armstrong" suggestion, I used a different char length, I went with a char(37).
c# mysql entity-framework ef-code-first ef-migrations
c# mysql entity-framework ef-code-first ef-migrations
edited Nov 11 at 5:07
asked Nov 10 at 13:46
devpro101
155119
155119
1
Can you post the full stack trace? Are you sure it’s not your own code that’s throwing the exception? You say you are using this property to store a guid. Are you converting the property to/from actual guid type somewhere in your code?
– Dave M
Nov 10 at 14:29
I can’t now, I am still on my way home from work.. i will give you that tomorrow..
– devpro101
Nov 10 at 15:21
add a comment |
1
Can you post the full stack trace? Are you sure it’s not your own code that’s throwing the exception? You say you are using this property to store a guid. Are you converting the property to/from actual guid type somewhere in your code?
– Dave M
Nov 10 at 14:29
I can’t now, I am still on my way home from work.. i will give you that tomorrow..
– devpro101
Nov 10 at 15:21
1
1
Can you post the full stack trace? Are you sure it’s not your own code that’s throwing the exception? You say you are using this property to store a guid. Are you converting the property to/from actual guid type somewhere in your code?
– Dave M
Nov 10 at 14:29
Can you post the full stack trace? Are you sure it’s not your own code that’s throwing the exception? You say you are using this property to store a guid. Are you converting the property to/from actual guid type somewhere in your code?
– Dave M
Nov 10 at 14:29
I can’t now, I am still on my way home from work.. i will give you that tomorrow..
– devpro101
Nov 10 at 15:21
I can’t now, I am still on my way home from work.. i will give you that tomorrow..
– devpro101
Nov 10 at 15:21
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
EntityFramework automatically maps CHAR(36) TO GUID for MySQL per Entity Framework Data Type Mapping
Try adding old guids=true
to your connection string per C# & MySQL Connector: Guid should contain 32 digits with 4 dashes
That’s interesting but I just have one question regarding that solution. Besides the Id property that I have, I am also using a RowVersion property of type System.Guid as EF’s concurrency token. Since I will be instructing EF to interpret guids as System.String, I am guesing it would also affect EF’s interpretation of my RowVersion property?..
– devpro101
Nov 10 at 15:27
1
I’m not aware of any way to disable the auto mapping on a case by case basis. One other option is to use CHAR(37), annoying, but it would get the job done.
– Jason Armstrong
Nov 10 at 16:31
Interesting, can’t wait to try your suggestions tomorrow. Will update you tomorrow ASAP thanks ^_^
– devpro101
Nov 10 at 17:57
hi. i am trying out your suggestion now but its not working. old guids is supposed to be one word (oldguids) but doesn't work. if i update the database using migration, visual studio crashes. if i make the length anything other than 36, i get the same error as before, EF complains when i use a non-guid formatted string
– devpro101
Nov 11 at 4:49
1
My bad, I forgot to change one Id property to a new length. I take back my last comment. My code is working now. Thank you so much.
– devpro101
Nov 11 at 5:03
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
EntityFramework automatically maps CHAR(36) TO GUID for MySQL per Entity Framework Data Type Mapping
Try adding old guids=true
to your connection string per C# & MySQL Connector: Guid should contain 32 digits with 4 dashes
That’s interesting but I just have one question regarding that solution. Besides the Id property that I have, I am also using a RowVersion property of type System.Guid as EF’s concurrency token. Since I will be instructing EF to interpret guids as System.String, I am guesing it would also affect EF’s interpretation of my RowVersion property?..
– devpro101
Nov 10 at 15:27
1
I’m not aware of any way to disable the auto mapping on a case by case basis. One other option is to use CHAR(37), annoying, but it would get the job done.
– Jason Armstrong
Nov 10 at 16:31
Interesting, can’t wait to try your suggestions tomorrow. Will update you tomorrow ASAP thanks ^_^
– devpro101
Nov 10 at 17:57
hi. i am trying out your suggestion now but its not working. old guids is supposed to be one word (oldguids) but doesn't work. if i update the database using migration, visual studio crashes. if i make the length anything other than 36, i get the same error as before, EF complains when i use a non-guid formatted string
– devpro101
Nov 11 at 4:49
1
My bad, I forgot to change one Id property to a new length. I take back my last comment. My code is working now. Thank you so much.
– devpro101
Nov 11 at 5:03
add a comment |
up vote
2
down vote
accepted
EntityFramework automatically maps CHAR(36) TO GUID for MySQL per Entity Framework Data Type Mapping
Try adding old guids=true
to your connection string per C# & MySQL Connector: Guid should contain 32 digits with 4 dashes
That’s interesting but I just have one question regarding that solution. Besides the Id property that I have, I am also using a RowVersion property of type System.Guid as EF’s concurrency token. Since I will be instructing EF to interpret guids as System.String, I am guesing it would also affect EF’s interpretation of my RowVersion property?..
– devpro101
Nov 10 at 15:27
1
I’m not aware of any way to disable the auto mapping on a case by case basis. One other option is to use CHAR(37), annoying, but it would get the job done.
– Jason Armstrong
Nov 10 at 16:31
Interesting, can’t wait to try your suggestions tomorrow. Will update you tomorrow ASAP thanks ^_^
– devpro101
Nov 10 at 17:57
hi. i am trying out your suggestion now but its not working. old guids is supposed to be one word (oldguids) but doesn't work. if i update the database using migration, visual studio crashes. if i make the length anything other than 36, i get the same error as before, EF complains when i use a non-guid formatted string
– devpro101
Nov 11 at 4:49
1
My bad, I forgot to change one Id property to a new length. I take back my last comment. My code is working now. Thank you so much.
– devpro101
Nov 11 at 5:03
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
EntityFramework automatically maps CHAR(36) TO GUID for MySQL per Entity Framework Data Type Mapping
Try adding old guids=true
to your connection string per C# & MySQL Connector: Guid should contain 32 digits with 4 dashes
EntityFramework automatically maps CHAR(36) TO GUID for MySQL per Entity Framework Data Type Mapping
Try adding old guids=true
to your connection string per C# & MySQL Connector: Guid should contain 32 digits with 4 dashes
answered Nov 10 at 14:30
Jason Armstrong
3097
3097
That’s interesting but I just have one question regarding that solution. Besides the Id property that I have, I am also using a RowVersion property of type System.Guid as EF’s concurrency token. Since I will be instructing EF to interpret guids as System.String, I am guesing it would also affect EF’s interpretation of my RowVersion property?..
– devpro101
Nov 10 at 15:27
1
I’m not aware of any way to disable the auto mapping on a case by case basis. One other option is to use CHAR(37), annoying, but it would get the job done.
– Jason Armstrong
Nov 10 at 16:31
Interesting, can’t wait to try your suggestions tomorrow. Will update you tomorrow ASAP thanks ^_^
– devpro101
Nov 10 at 17:57
hi. i am trying out your suggestion now but its not working. old guids is supposed to be one word (oldguids) but doesn't work. if i update the database using migration, visual studio crashes. if i make the length anything other than 36, i get the same error as before, EF complains when i use a non-guid formatted string
– devpro101
Nov 11 at 4:49
1
My bad, I forgot to change one Id property to a new length. I take back my last comment. My code is working now. Thank you so much.
– devpro101
Nov 11 at 5:03
add a comment |
That’s interesting but I just have one question regarding that solution. Besides the Id property that I have, I am also using a RowVersion property of type System.Guid as EF’s concurrency token. Since I will be instructing EF to interpret guids as System.String, I am guesing it would also affect EF’s interpretation of my RowVersion property?..
– devpro101
Nov 10 at 15:27
1
I’m not aware of any way to disable the auto mapping on a case by case basis. One other option is to use CHAR(37), annoying, but it would get the job done.
– Jason Armstrong
Nov 10 at 16:31
Interesting, can’t wait to try your suggestions tomorrow. Will update you tomorrow ASAP thanks ^_^
– devpro101
Nov 10 at 17:57
hi. i am trying out your suggestion now but its not working. old guids is supposed to be one word (oldguids) but doesn't work. if i update the database using migration, visual studio crashes. if i make the length anything other than 36, i get the same error as before, EF complains when i use a non-guid formatted string
– devpro101
Nov 11 at 4:49
1
My bad, I forgot to change one Id property to a new length. I take back my last comment. My code is working now. Thank you so much.
– devpro101
Nov 11 at 5:03
That’s interesting but I just have one question regarding that solution. Besides the Id property that I have, I am also using a RowVersion property of type System.Guid as EF’s concurrency token. Since I will be instructing EF to interpret guids as System.String, I am guesing it would also affect EF’s interpretation of my RowVersion property?..
– devpro101
Nov 10 at 15:27
That’s interesting but I just have one question regarding that solution. Besides the Id property that I have, I am also using a RowVersion property of type System.Guid as EF’s concurrency token. Since I will be instructing EF to interpret guids as System.String, I am guesing it would also affect EF’s interpretation of my RowVersion property?..
– devpro101
Nov 10 at 15:27
1
1
I’m not aware of any way to disable the auto mapping on a case by case basis. One other option is to use CHAR(37), annoying, but it would get the job done.
– Jason Armstrong
Nov 10 at 16:31
I’m not aware of any way to disable the auto mapping on a case by case basis. One other option is to use CHAR(37), annoying, but it would get the job done.
– Jason Armstrong
Nov 10 at 16:31
Interesting, can’t wait to try your suggestions tomorrow. Will update you tomorrow ASAP thanks ^_^
– devpro101
Nov 10 at 17:57
Interesting, can’t wait to try your suggestions tomorrow. Will update you tomorrow ASAP thanks ^_^
– devpro101
Nov 10 at 17:57
hi. i am trying out your suggestion now but its not working. old guids is supposed to be one word (oldguids) but doesn't work. if i update the database using migration, visual studio crashes. if i make the length anything other than 36, i get the same error as before, EF complains when i use a non-guid formatted string
– devpro101
Nov 11 at 4:49
hi. i am trying out your suggestion now but its not working. old guids is supposed to be one word (oldguids) but doesn't work. if i update the database using migration, visual studio crashes. if i make the length anything other than 36, i get the same error as before, EF complains when i use a non-guid formatted string
– devpro101
Nov 11 at 4:49
1
1
My bad, I forgot to change one Id property to a new length. I take back my last comment. My code is working now. Thank you so much.
– devpro101
Nov 11 at 5:03
My bad, I forgot to change one Id property to a new length. I take back my last comment. My code is working now. Thank you so much.
– devpro101
Nov 11 at 5:03
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239607%2fwhy-is-ef-treating-a-char-property-as-an-actual-guid%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Can you post the full stack trace? Are you sure it’s not your own code that’s throwing the exception? You say you are using this property to store a guid. Are you converting the property to/from actual guid type somewhere in your code?
– Dave M
Nov 10 at 14:29
I can’t now, I am still on my way home from work.. i will give you that tomorrow..
– devpro101
Nov 10 at 15:21