0

I'm developing website using MVC3 using model first approach.

I have a field in my "Employee" model that is "username". And this field is neither a unique field nor foreign key. my application requirement is username should be unique, and if user enters duplicate value then it show error message.

For my mvc project, i created entity framework as a class library and added its reference in the mvc project for accessing model classes and all this related to entity framework i created for my website.

How to do this in model itself using EF model first approach ?

Priyanka
  • 2,802
  • 14
  • 55
  • 88

2 Answers2

0

I think you can set this requirement by using the [Key] notation above the user field in the model. But I'm not sure :)

Stripps
  • 156
  • 3
0

i suffered that type of problem in my project. i was working on asp.net application and it's a job portal so candidate email Id is unique so i check like that and solve this problem. ite's below

In the form

<div id="dvErrorMessage" style="display: none;" title="Message">
    <p>
        Please Registered with different EmailId.This is allready exists.</p>
</div>


 $("#txtEmailID").blur(function (e) {
            if (document.getElementById("txtEmailID").value == "") {
                document.getElementById("txtEmailID").focus();
                document.getElementById("lblEmailID").innerHTML = "Please Enter Email ID";
                document.getElementById("imgEmailID").src = "images/cross-sign.png";
            }
else {
                e.preventDefault();
                var EmailID = $("#txtEmailID").val();
                var url = "JsonData.asmx/GetCompanyEmailAccessbility";
                var emailformate = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                $.ajax({
                    type: "POST",
                    url: url,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    data: '{EmailID:"' + EmailID + '"}',
                    success: function (msg) {
                        var data = msg.d;
                        if (data == 0) {
                            if (emailformate.test(document.getElementById("txtEmailID").value) == false) {
                                document.getElementById("imgEmailID").src = "images/cross-sign.png";
                                document.getElementById("lblEmailID").innerHTML = "Please Enter Valid Email ID";
                                document.getElementById("txtEmailID").focus();
                                return false;
                            }
                            else {
                                document.getElementById("imgEmailID").src = "images/right-sign.png"
                            }
                        }
                        else {
                            if ($("#dvErrorMessage").css('display') == 'none') {
                                $("#dvErrorMessage").css('display', 'block');
                                $("#dvErrorMessage").dialog({
                                    autoOpen: false,
                                    modal: true,
                                    width: 400,
                                    resizable: false,
                                    height: 200,
                                    buttons: {
                                        "OK": function (e) {
                                            e.preventDefault();
                                            $(this).dialog('close');
                                            document.getElementById("txtEmailID").focus();
                                            document.getElementById("imgEmailID").src = "images/cross-sign.png";
                                        }
                                    }
                                });

                                $("#dvErrorMessage").dialog('open');
                            }
                            else {
                                $("#dvErrorMessage").dialog({
                                    autoOpen: false,
                                    modal: true,
                                    width: 400,
                                    resizable: false,
                                    height: 200,
                                    buttons: {
                                        "OK": function (e) {
                                            e.preventDefault();
                                            $(this).dialog('close');
                                            document.getElementById("txtEmailID").focus();
                                            document.getElementById("imgEmailID").src = "images/cross-sign.png";
                                        }
                                    }
                                });

                                $("#dvErrorMessage").dialog('open');
                            }
                        }
                    }
                });
            }
        });
    }

in my web services

 public List<CompanyData> GetCompanyEmailAccessbility(string EmailID)
        {
            PresenterData objpreseData = new PresenterData();
            LMGDAL.db_LMGEntities dbData = new db_LMGEntities();
            var dataAccessibility = dbData.tblCompanies.Where(x => x.CompanyEmailId == EmailID).ToList().Select(item => new CompanyData { EmailID = item.CompanyEmailId }).ToList();
            return dataAccessibility.ToList();
        }
Rajpurohit
  • 1,951
  • 2
  • 16
  • 19