none
Entity Code First String FK values Uppercase or Lowercase RRS feed

  • Question

  • FK is linked to tbWarehouseUser 1 recording. There are both lowercase and uppercase values in the tbWarehouseUser table.

    How can I solve this problem ?

    The classes are as follows;

    private void Form1_Load(object sender, EventArgs e)
        {
            var model = new Model1();
            var tbWarehouse = model.tbWarehouse.Where(c=> c.sWarehouse == "A1").FirstOrDefault();
    
            var tbWarehouseUserList = tbWarehouse.tbWarehouseUser.ToList();
        }
    
    

    tbWarehouseUserList.Count record count 1.

    1 record is coming. This problem causes sourced?

    public partial class tbWarehouse
    {
    
        tbWarehouse()
        {
            tbWarehouseUser = new HashSet<tbWarehouseUser>();
        }
    
        [Key]
        [StringLength(4)]
        public string sWarehouse { get; set; }
        public string WarehouseDescription { get; set; }
    
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<tbWarehouseUser> tbWarehouseUser { get; set; }
    
    }
    
    
    
    public partial class tbWarehouseUser
    {
        [Key]
        [Column(Order = 0)]
        [StringLength(4)]
        public string sUserName { get; set; }        
    
        [Key]
        [Column(Order = 1)]
        [StringLength(4)]
        public string sWarehouseName { get; set; }
    
        public string sUserPass { get; set; }
    
    
        public virtual tbWarehouse tbWarehouse { get; set; }
    
    
    }
    
    
    public partial class Model1 : DbContext
    {
        public Model1()
            : base("name=Model1")
        {
        }
    protected void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<tbWarehouse>()
                .Property(e => e.sWarehouse)
                .IsFixedLength()
                .IsUnicode(false);
    
            modelBuilder.Entity<tbWarehouse>()
                .Property(e => e.WarehouseDescription)
                .IsUnicode(false);
    
            modelBuilder.Entity<tbWarehouse>()
                .HasMany(e => e.tbWarehouseUser)
                .WithRequired(e => e.tbWarehouse)
                .HasForeignKey(e => e.sWarehouseName)
                .WillCascadeOnDelete(false);
    
            modelBuilder.Entity<tbWarehouseUser>()
                .Property(e => e.sUserName)
                .IsFixedLength()
                .IsUnicode(false);
    
            modelBuilder.Entity<tbWarehouseUser>()
                .Property(e => e.sWarehouseName)
                .IsFixedLength()
                .IsUnicode(false);
    
            modelBuilder.Entity<tbWarehouseUser>()
                .Property(e => e.sUserPass)
                .IsFixedLength()
                .IsUnicode(false);
        }
    }

    • Moved by CoolDadTx Wednesday, January 25, 2017 3:07 PM EF related
    Wednesday, January 25, 2017 12:19 PM

All replies

  • Hello,

    Take a look at the following page where they talk about this issue. On a side note, I created a method (using model first but should work in code first) found in the following MSDN code sample. Look at CompanyNameFilter in Customers.cs which is used in tangent with a language extension method found in Extensions.cs, Filter.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 25, 2017 12:42 PM
  • I tested. But it did not.

    The connected user table again comes up to one count.

    I have to find another solution.

    Wednesday, January 25, 2017 1:54 PM
  • Unsure why, here is a demo of my method mentioned in my first reply. one starts with lower a, the other with A.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 25, 2017 3:57 PM
  • I can do things that you talk about as follows:

    var tbWarehouse = model.tbWarehouse.Where(c=> c.sWarehouse == "A1").FirstOrDefault();

    var tbWarehouseUser = model.tbWarehouseUser.Where(c=> c.sWarehouseName == tbWarehouse.sWarehouse.ToLower() || c.sWarehouseName == tbWarehouse.sWarehouse.ToUpper()).ToList();

    or

    var tbWarehouseUser_1 = model.tbWarehouseUser.Where(c=> c.sWarehouseName.ToLower().StartsWith(tbWarehouse.sWarehouse.ToLower(),StringComparison.CurrentCulture)).ToList();

    No problem.

    But, I want to use is the following.

    var list = tbWarehouse.tbWarehouseUser.ToList();

    list same count one.

    I hope you know what I mean?

    Wednesday, January 25, 2017 7:20 PM
  • Are you a reply writer????
    Monday, January 30, 2017 12:03 PM
  • Are you a reply writer????
    I'm assuming you are talking to me. I don't know what a reply writer is. I'm simply a contributor (not a Microsoft employee) to these forums and a moderator on several forums. 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 30, 2017 12:22 PM
  • How do I get to a Microsoft employee?
    Thursday, February 2, 2017 6:42 AM
  • Hi Ersin Şentürk,

    Sorry for late reply.

    Based on your description, I create a simple demo and reproduce your issue on my side. it seems that it is a entity framework issue, which mentioned on the following article.

    https://www.sswug.org/bentaylor/editorials/entity-framework-foreign-keys-and-strings-as-primary-key/

    I would suggest that you could post a feedback on the following link.

    https://connect.microsoft.com/VisualStudio/Feedback 

    In addition, you could use the workaround as you mentioned. you could also use the following workaround.

                    var tbWarehouseUserList = (from p in model.tbWarehouses
                                              join u in model.tbWarehouseUsers on p.sWarehouse equals u.sWarehouseName
                                              where p.sWarehouse == "A1"
                                              select u).ToList();

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 10, 2017 3:07 AM
    Moderator