2017-03-07 113 views
0

我曾在我的方法的錯誤,當我嘗試添加到數據庫中我的接口,它給我的錯誤,無法從接口轉換爲

參數1:無法從「ForumSite.ActionsAndMethods轉換。 Registration.IRegistration'到'ForumSite.Models.User'。

這裏是IRegistration代碼:

using ForumSite.Models; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ForumSite.ActionsAndMethods.Registration 
{ 
public interface IRegistration 
{ 
    int UserId { get; } 
    string Email { get; } 
    string Password { get; } 
    string FirstName { get; } 
    string LastName { get; } 
    DateTime Birthday { get; } 
    DateTime DateCreated { get; set; } 
    string MobileNumber { get; } 
    string Address { get; } 
    int UserIsDeleted { get; set; } 
    int UserRoleId { get; set; } 
    UserRole UserRole { get; } 
} 
} 

而且這是在我的模型代碼:

namespace ForumSite.Models 
{ 
using ActionsAndMethods.Registration; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 

public partial class User : IRegistration 
{ 
    public int UserId { get; set; } 
    [Display(Name = "Email Address")] 
    [Required(ErrorMessage = "This field required.")] 
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z]+)\.([a-zA-Z]{2,5})$", ErrorMessage = "Enter Valid Email Address")] 
    [StringLength(50, MinimumLength = 8, ErrorMessage = "8 to 50 characters only")] 
    public string Email { get; set; } 

    [Display(Name = "Password")] 
    [Required(ErrorMessage = "This field required.")] 
    [RegularExpression(@"^[a-zA-Z0-9]+$", ErrorMessage = "Alphanumeric characters only")] 
    [StringLength(50, MinimumLength = 8, ErrorMessage = "8 to 50 characters only")] 
    public string Password { get; set; } 

    [Display(Name = "Confirm Password")] 
    [Required(ErrorMessage = "This field required.")] 
    [RegularExpression(@"^[a-zA-Z0-9]+$", ErrorMessage = "Alphanumeric characters only")] 
    [StringLength(50, MinimumLength = 8, ErrorMessage = "8 to 50 characters only")] 
    public string PasswordConfirm { get; set; } 

    [Display(Name = "First Name")] 
    [Required(ErrorMessage = "This field required.")] 
    [RegularExpression(@"^[a-zA-Z\s]+$", ErrorMessage = "Letters Only.")] 
    public string FirstName { get; set; } 

    [Display(Name = "Last Name")] 
    [Required(ErrorMessage = "This field required.")] 
    [RegularExpression(@"^[a-zA-Z\s]+$", ErrorMessage = "Letters Only.")] 
    public string LastName { get; set; } 

    [Display(Name = "Birthday")] 
    [Required(ErrorMessage = "This field required.")] 
    public DateTime Birthday { get; set; } 
    public DateTime DateCreated { get; set; } 

    [Display(Name = "Mobile Number")] 
    [Required(ErrorMessage = "This field required.")] 
    [RegularExpression(@"^[0-9]+$", ErrorMessage = "Numeric input only.")] 
    public string MobileNumber { get; set; } 

    [Display(Name = "Address")] 
    [Required(ErrorMessage = "This field required.")] 
    public string Address { get; set; } 
    public int UserIsDeleted { get; set; } 
    public int UserRoleId { get; set; } 

    public UserRole UserRole { get; set; } 
} 
} 

最後,我的方法,將用戶添加到我的數據庫:

using ForumSite.ActionsAndMethods.Registration; 
using ForumSite.Models; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace ForumSite.ActionsAndMethods 
{ 
public class RegisterAction : IRegistration 
{ 
    ForumDBEntities ent = new ForumDBEntities(); 
    public void Registration(IRegistration reg) 
    { 
     reg.DateCreated = DateTime.Now; 
     reg.UserRoleId = 1; 
     reg.UserIsDeleted = 0; 

     ent.Users.Add(reg); 
     ent.SaveChanges(); 
    } 

    string IRegistration.Address { get; } 
    int IRegistration.UserId { get; } 
    string IRegistration.Email { get; } 
    string IRegistration.Password { get; } 
    string IRegistration.FirstName { get; } 
    string IRegistration.LastName { get; } 
    DateTime IRegistration.Birthday { get; } 
    DateTime IRegistration.DateCreated { get; set; } 
    string IRegistration.MobileNumber { get; } 
    int IRegistration.UserIsDeleted { get; set; } 
    int IRegistration.UserRoleId { get; set; } 

    UserRole IRegistration.UserRole { get; } 
} 

} 

我不知道是什麼原因導致這個錯誤?

+6

雖然每個User都實現IRegistration,但不是每個IRegistration實例都是User。 – DavidG

+1

您還需要顯示實際引發錯誤的行 –

+0

您能告訴我們錯誤發生的代碼嗎? – phuzi

回答

4

儘管每個User實現了IRegistration,但並非每個IRegistration實例都是User

例如,您的RegisterAction實施IRegistration。所以,如果你想做的事是可能的,你可以在理論上有這樣的代碼:

RegisterAction action = GetRegisterAction(); 

RegisterAction action2 = new RegisterAction(action); 

這意味着,當你來幹什麼:

ent.Users.Add(reg); 

它會失敗,因爲reg不是User

你可以解決它是這樣的:

var user = reg as User; 
if(user != null) 
{ 
    ent.Users.Add(user); 
} 

但實際上,因爲它似乎很奇怪,做這個你應該看看你的結構第一。

+0

謝謝先生。代碼終於奏效了。但是我仍然對數據庫有一個錯誤,說「驗證失敗了一個或多個實體。有關更多詳細信息,請參閱'EntityValidationErrors'屬性。」 :D –

0

你還沒有告訴確切的是你在哪裏得到編譯錯誤,但我的猜測是它在Registration方法ent.Users.Add(reg)行。

要解決的編譯錯誤,你應該參數更改爲Registration方法從IRegistrationUser

public void Registration(User reg) 
{ 
    reg.DateCreated = DateTime.Now; 
    reg.UserRoleId = 1; 
    reg.UserIsDeleted = 0; 

    ent.Users.Add(reg); 
    ent.SaveChanges(); 
} 

然而,這可能進一步觸發另一個編譯錯誤了調用鏈,如果你試圖傳遞一個IRegistration而不是UserRegistration方法。

雖然User可以(而且確實)實現IRegistration你不能假設任何實施IRegistrationUser。如果你的代碼是基於這個假設,你將不得不解決這個問題來解決你的問題。

+0

我也試過這段代碼,它工作。感謝您的幫助! :d –

相關問題