0

I have been working on this for days because I use emoji strings as unique tokens in DbBontext. the following code has been used against SqlServer 2019 express with Visual Studio 2019 as well as my older versions. My goal was to create random 5 emoji strings, store them in SqlServer using Entity Framework .NET Framework. Then be able to use DbContext to query them. There is some problem with LINQ. It is exampled here. I created a SqlServer DB with two columns, one the PK and one an indexed column. Both ad Varchar(255). I popoulate them with emoji 5 character emoji pairs that are identical. ie context.RomanEmpires.Add(new RomanEmpire() { Romulus = "♥⚡♥⌚㊙", Remus = "♥⚡♥⌚㊙" }); They all successfully insert into the DB and are visible using SSMS 2019 However the following code yields unusable results.

        var listOfEntries = context.RomanEmpires.ToList();
        foreach (var line in listOfEntries)
        {
            context.RomanEmpires.Remove(line); 
        }
        context.SaveChanges();                       
        context.RomanEmpires.Add(new RomanEmpire() { Romulus = "♥⚡♥⌚㊙", Remus = "♥⚡♥⌚㊙" });            
        context.RomanEmpires.Add(new RomanEmpire() { Romulus = "‍‍", Remus = "‍‍" });           
        context.RomanEmpires.Add(new RomanEmpire() { Romulus = "⚡⌚", Remus = "⚡⌚" });            
        context.SaveChanges();
        string toMatch = "";
        var matchedEntry = context.RomanEmpires.FirstOrDefault(e => e.Romulus == toMatch);          
        var listofMatches = context.RomanEmpires.Where(e => e.Romulus == toMatch).Count();
        var listofNotMatches = context.RomanEmpires.Where(e => e.Romulus != toMatch).Count();
        context.RomanEmpires.Add(new RomanEmpire() { Romulus = "‍", Remus = "‍" });

string toMatch = ""; finds a match om matched entry the "Add in the last line of code finds a duplicate insertion exception with a primary key violation. Therefore, the SqlServer evaluates a single Remus ‍ in conflict with ‍‍

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
user3594395
  • 181
  • 4
  • 7
  • Please format your code so we can read it. – HazardousGlitch Jul 25 '20 at 01:39
  • 1
    Use `Nvarchar`. – Alexander Petrov Jul 25 '20 at 01:47
  • It might be related to the collation of your DB. – Bruno Jul 25 '20 at 02:02
  • Nvarchar was used on both Db columns in SqlServer. I would agree that it could be the collation sequence but I couldn't apply the one used in MySql which might have worked. It wasn't in my dropdown. – user3594395 Jul 26 '20 at 17:12
  • I was able to work around the issue by encoding each Emoji String to a UTF-8 Encoded String using this contributed solution https://stackoverflow.com/questions/1615559/convert-a-unicode-string-to-an-escaped-ascii-string. These generated Encoded Strings are apparently unique and queryable using SqlServer and EntityFramework Db Context. If you are interested in observing the purpose please text to US 239-732-5500. The goal is to create a unique emoji token that is used to authenticate a smartphone as an identity in AspNet Identity. – user3594395 Jul 26 '20 at 17:37

0 Answers0