0

I'm trying to get my mysql data in ASP.net MVC3.

The mysql Database Name is supply_db and table name is xcart_orders.

ASP.net code is like below,

(Im just following my book, and just switch to my DB info but it does not work :( )

(I will omit using and namespace)

Web.Config File,

<add name="EFMysqlContext" connectionString="server=XXX.XXX.XXX.XX;User Id=root;pwd=xxx;Persist Security Info=True;database=supply_db"
         providerName="Mysql.Data.MySqlClient" />

Abstract/IXcartOrdersRepository.cs

public interface IXcartOrdersRepository
{
    IQueryable<XcartOrder> xcart_orders { get; }
}

/Concrete/EFXcartOrderRepository.cs

public class EFXcartOrdersRepository : IXcartOrdersRepository
{
private EFMysqlContext context = new EFMysqlContext();

public IQueryable<XcartOrder> xcart_orders 
{
    get { return context.xcart_orders; }  // I thought the 'xcart_orders' should be match with db table name, isn't it?
}
}

/Entities/XcartOrder.cs

public class XcartOrder
{
    [Key]
    public int orderid { get; set; }
    public string login { get; set; }
    public string membership { get; set; }
    public decimal subtotal { get; set; }
}

and In my controller,

IXcartOrdersRepository XcartOrdersRepository = new EFXcartOrdersRepository();
int orderCnt = XcartOrdersRepository.xcart_orders.Count();

then error occur, the error message say "{"Table 'supply_db.XcartOrders' doesn't exist"}"

I think I could connect to db, but couldn't get the table.

anybody know which part do I need to change?

Thank you!

1 Answer 1

1

can you decorate your Xcartorder class with the Table attribute to explicitly specify the desired name?

[Table("xcart_orders")]
public class XcartOrder
{
    ...

edit: attribute syntax

Sign up to request clarification or add additional context in comments.

6 Comments

Firstly thank you for your answer, but there is an error, the error message say "'Name' is not a valid named attribute argument. Named attribute arguments must be fields which are not readonly, static, or const, or read-write properties which are public and not static."
and /Concrete/EFXcartOrderRepository.cs file communicate to Database, isn't it? I thought EFXcartOrderRepository.cs has something wrong... @.@ Could you help me a little more plz?
Yes, the repository interacts with the database, but it's the object's properties (via conventions) and DataAnnotations (to explicitly map) that allow it to map to your DB schema. Specifying the table on the class or any column names on the properties that differ from what is expected by convention lets you override what EF expects.
aha! it maps DB schema!, I just changed the file name /Entities/XcartOrder.cs to xcart_orders.cs, then it works :) . Also [Table("xcart_orders")] is works too! Of course better using singular naming :) Thank you very much!
May I ask you one more quick question? In EFXcartOrderRepository.cs, "return context.xcart_orders;" <= the "xcart_orders" should be matched with actual DB table name. is it correct?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.