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).










share|improve this question




















  • 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















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).










share|improve this question




















  • 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













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).










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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






share|improve this answer





















  • 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











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',
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
});


}
});














 

draft saved


draft discarded


















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
































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






share|improve this answer





















  • 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















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






share|improve this answer





















  • 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













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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ