Why is EF treating a char property as an actual guid?
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 |
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 |
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
1651110
1651110
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
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
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
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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
682313
682313
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 |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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